5
\$\begingroup\$

I'm looking for the best solution.

Here is a response from server and i need to get Organizations list:

Content-Type:application/json;charset=UTF-8
 {
 "code": 0,
 "message": "success",
 "organizations": [
 {
 "organization_id": "10234695",
 "name": "Zillum",
 "contact_name": "John Smith",
 "email": "[email protected]",
 "is_default_org": false,
 "language_code": "en",
 "fiscal_year_start_month": 0,
 "account_created_date": "2016-02-18",
 "time_zone": "PST",
 "is_org_active": true,
 "currency_id": "460000000000097",
 "currency_code": "USD",
 "currency_symbol": "$",
 "currency_format": "###,##0.00",
 "price_precision": 2
 },
 {...},
 {...}
 ]

Here is my convert method:

var contentJson = await SendRequest(request);
 var contentJo = (JObject)JsonConvert.DeserializeObject(contentJson);
 var organizationsJArray = contentJo["organizations"]
 .Value<JArray>();
 var organizations = organizationsJArray.ToObject<List<Organization>>();

Code works, but I'm looking for a better Convert Method. Can I do without converting to JArray?

t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
asked Jun 4, 2018 at 8:53
\$\endgroup\$
5
  • \$\begingroup\$ You're not handling errors and corner-cases but, well, that might be OK...is there a specific goal? To me this small snippet looks OK (I have, maybe, just few opinionated minor style comments) but you may have some more specific issues in mind (performance? maintainability?) \$\endgroup\$ Commented Jun 4, 2018 at 9:35
  • \$\begingroup\$ You can use the List<Organization> in the same way as a generic parameter for the DeserializeObject method. There's no need to create the JObject first. \$\endgroup\$ Commented Jun 4, 2018 at 9:38
  • \$\begingroup\$ @t3chb0t it cannot be convert directly, because root object it's not an Array \$\endgroup\$ Commented Jun 4, 2018 at 10:15
  • \$\begingroup\$ oh, ok, then put it in another object that has a property of this type. \$\endgroup\$ Commented Jun 4, 2018 at 10:16
  • \$\begingroup\$ @AdrianoRepetti Yeah, thanks a lot. I'm using error handling. I thought that there is a shorter conversion, because I'm not familiar with the methods Newtonsoft.Json \$\endgroup\$ Commented Jun 4, 2018 at 10:23

2 Answers 2

5
\$\begingroup\$

Given that you are already using the ToObject, consider simplifying the code for readability and the advantage of not having to convert anything.

var contentJson = await SendRequest(request);
dynamic response = JsonConvert.DeserializeObject(contentJson); 
List<Organization> organizations = response.organizations.ToObject<List<Organization>>();

The actual response appears to be of no concern so using a dynamic simplifies things. Converting back to strongly typed objects by calling ToObject<T> was a good choice and should work out fine.

answered Jun 5, 2018 at 0:43
\$\endgroup\$
1
\$\begingroup\$

Json.Net will (by default) accept missing fields without a problem when doing typed deserialization, so I believe you can do:

class ContentData
{
 public List<Organization> organizations;
}
...
var contentData = JsonConvert.DeserializeObject<ContentData>(contentJson);
DoSomething(contentData.organizations);
answered Jun 5, 2018 at 0:55
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.