I am currently getting information back from a web api in the form of application/json data. The issue I am running into is that sometimes I get an error using JObject.Parse(response) and I don't with JArray.Parse(response), and sometimes it is vice versa. I have been looking around for a bit and haven't been able to figure out why some data returned has [] around it and some does not. That being said, is there a way to parse the data consistently whether it has [] around it or not? The only thing I can currently think is by checking to see if the response string starts with a '[', and use JArray.Parse if it does, and then use JObject.Parse if it doesn't but that seems like a kinda ugly solution. I'm still pretty new to API's in general, so any advice would be appreciate.
(Edit: Language is C#)
Code Snippet:
using (WebClient webClient = new WebClient())
{
webClient.Headers.Add("Authorization", "Basic ***");
webClient.Headers.Add("clientId", clientId);
webClient.Headers.Add("Content-Type", "application/json");
string agreementResponse = webClient.DownloadString(urlAgreementBase + urlAgreementParams);
if (response == null)
{
throw new InvalidPluginExecutionException("Failed to retrieve company. Response was empty.");
}
var agreementObject = JArray.Parse(response);
}
JSON:
[
{
"id": int,
"name": "string",
"type": {
"id": int,
"name": "string",
"_info": {
"type_href": "url string"
}
},
"company": {
"id": int,
"identifier": "string",
"name": "string",
"_info": {
"company_href": "url string"
}
},
"contact": {
"id": int,
"name": "string",
"_info": {
"contact_href": "url string"
}
},
"site": {
"id": int,
"name": "string",
"_info": {
"site_href": "url string"
}
},
"location": {
"id": int,
"name": "string",
"_info": {
"location_href": "url string"
}
},
"department": {
"id": int,
"identifier": "string",
"name": "string",
"_info": {
"department_href": "url string"
}
},
"restrictLocationFlag": bool,
"restrictDepartmentFlag": bool,
"startDate": "date string",
"noEndingDateFlag": bool,
"cancelledFlag": bool,
"sla": {
"id": int,
"name": "string",
"_info": {
"sla_href": "url string"
}
}
}
]