Hello to all!
For some time I have been trying to understand XML and JSON parsing with CRMScript as I always get into some trouble.
This time ain't different. Im trying to recieve JSON from a an API and failing to do any magic with it.
I've also seen this wonderful article by Michel but it doesent help my purpose.
The JSON looks like this:
[
{
"internalId":23870,
"number":"10000",
"name":"Customer name",
"status":"Active",
"mainAddress":{
"addressId":60253,
"addressLine1":null,
"addressLine2":null,
"addressLine3":null,
"postalCode":null,
"city":null,
"country":{
"id":"NO",
"name":"NORGE",
"errorInfo":null,
"metadata":{
"totalCount":0
}
},
"county":{
"id":null,
"name":null
}
},
"mainContact":{
"contactId":42131,
"name":"Customer name",
"attention":null,
"email":null,
"web":null,
"phone1":null,
"phone2":null,
"fax":null
},
"accountReference":null,
"parentRecord":{
"number":null,
"name":null
},
"customerClass":{
"id":"1",
"description":""
},
"creditTerms":{
"id":"14",
"description":"14 dager netto"
},
"currencyId":"NOK",
"creditVerification":"Disabled",
"creditLimit":0.0000,
"creditDaysPastDue":0,
"invoiceAddress":{
"addressId":60253,
"addressLine1":null,
"addressLine2":null,
"addressLine3":null,
"postalCode":null,
"city":null,
"country":{
"id":"NO",
"name":"NORGE",
"errorInfo":null,
"metadata":{
"totalCount":0
}
},
"county":{
"id":null,
"name":null
}
},
"invoiceContact":{
"contactId":42131,
"name":"Customer name",
"attention":null,
"email":null,
"web":null,
"phone1":null,
"phone2":null,
"fax":null
},
"printInvoices":true,
"acceptAutoInvoices":false,
"sendInvoicesByEmail":false,
"printStatements":true,
"sendStatementsByEmail":false,
"printMultiCurrencyStatements":true,
"statementType":"OpenItem",
"deliveryAddress":{
"addressId":60253,
"addressLine1":null,
"addressLine2":null,
"addressLine3":null,
"postalCode":null,
"city":null,
"country":{
"id":"NO",
"name":"NORGE",
"errorInfo":null,
"metadata":{
"totalCount":0
}
},
"county":{
"id":null,
"name":null
}
},
"deliveryContact":{
"contactId":42131,
"name":"Customer name",
"attention":null,
"email":null,
"web":null,
"phone1":null,
"phone2":null,
"fax":null
},
"vatRegistrationId":null,
"corporateId":null,
"gln":null,
"vatZone":{
"id":"01",
"description":"Salg innenlands, avgiftspliktig",
"defaultVatCategory":null,
"defaultTaxCategory":{
"number":null,
"description":null
}
},
"location":{
"countryId":"NO",
"id":"Main",
"name":"Primary location"
},
"attributes":[
],
"lastModifiedDateTime":"2020-05-29T16:43:00.627",
"createdDateTime":"2020-05-29T16:43:00.627",
"priceClass":{
"id":null,
"description":null
},
"glAccounts":{
"customerLedgerAccount":{
"type":null,
"number":null,
"description":null
},
"salesAccount":{
"type":null,
"number":null,
"description":null
},
"salesNonTaxableAccount":{
"type":null,
"number":null,
"description":null
},
"salesEuAccount":{
"type":null,
"number":null,
"description":null
},
"salesExportAccount":{
"type":null,
"number":null,
"description":null
},
"salesSubaccount":{
"id":null,
"description":null
}
},
"invoiceToDefaultLocation":true,
"errorInfo":null,
"metadata":{
"totalCount":9
}
}
]
For me the (minimal) corresponding code should be like this:
struct CustomerInfo{
String InternalId;
Void fromJson(String json) {
this.fromXMLNode(parseJSON(json));
}
};
//Omitted the code for connecting to api.
String jsonResultFromApi = getJsonResult();
CustomerInfo custInfo;
custInfo.fromJson(jsonResultFromApi);
The code (obviusly) gives desired result only if there is 1 element in the JSON returned.
So the question is: How can i modify this to create all my structs/objects of type CustomerInfo i.e loop through the JSON.....