With
- Code: Select all
function OAuthXeroGET(client_id, secret) {
try
{
plugins.oauth // initialise
var oauth_api = OAuthAPI.XERO
var resource_url = "https://api.xero.com/api.xro/2.0/Organisation"
application.output(resource_url)
var method = Methods.GET;
var request = plugins.oauth.createOAuthRequest(oauth_api, client_id, secret, method);
request.setResourceUrl(resource_url);
request.execute();
var response = request.getResponse();
var json = response.getAttributeValue("Organisations")
application.output(json)
var vName = json[0]['LegalName']
var vPhone = json[0]['Phones'][0]['PhoneNumber']
}
catch(e)
{
application.output("OAuth error " + e)
}
}
I get this which looks like a JSON array,
- Code: Select all
[{"Phones":[{"PhoneCountryCode":"","PhoneAreaCode":"","PhoneNumber":"","PhoneType":"DDI"},{"PhoneCountryCode":"","PhoneAreaCode":"","PhoneNumber":"","PhoneType":"FAX"},{"PhoneCountryCode":"","PhoneAreaCode":"","PhoneNumber":"","PhoneType":"MOBILE"},{"PhoneCountryCode":"","PhoneAreaCode":"","PhoneNumber":"","PhoneType":"DEFAULT"}],"TaxNumber":"53003086616","Addresses":[{"PostalCode":"1234","Region":"","AddressLine4":"","AddressType":"POBOX","AttentionTo":"","Country":"Mozambique","AddressLine1":"23 Main Street","City":"Marineville","AddressLine2":"","AddressLine3":""},{"PostalCode":"","Region":"","AddressLine4":"","AddressType":"STREET","AttentionTo":"","Country":"","AddressLine1":"","City":"","AddressLine2":"","AddressLine3":""}],"OrganisationType":"COMPANY","OrganisationStatus":"ACTIVE","RegistrationNumber":"11111111138","OrganisationEntityType":"COMPANY","PeriodLockDate":"/Date(1214740800000+1200)/","BaseCurrency":"AUD","LineOfBusiness":"","Name":"Demo Company (AU)","LegalName":"Demo Company (AU)","FinancialYearEndDay":30,"PaysTax":true,"Timezone":"AUSEASTERNSTANDARDTIME","IsDemoCompany":true,"CountryCode":"AU","ShortCode":"dB62e","CreatedDateUTC":"/Date(1363381036907+1300)/","Version":"AU","FinancialYearEndMonth":6}]
And I can read it with the JSON editor http://braincast.nl/samples/jsoneditor/
but I'm unable to extract anything in Servoy such as
var vName = json[0]['LegalName']
Java class "org.json.JSONArray" has no public instance field or method named "0".
I've tried several things including changing it to a JSON object which is also readable with the JSON editor, yet still gives an error in Servoy.
var jsonobj = json.getJSONObject(0)
var vName = jsonobj[0]['LegalName']
Java class "org.json.JSONObject" has no public instance field or method named "0".
Can anyone help me with the correct syntax?