Click here to Skip to main content
15,903,831 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
{"orders": [
  "{\"orderid\": 12477}"
]}


can anybody tell me how to get orderid from orders object. I am getting error
Cannot access child value on Newtonsoft.Json.Linq.JValue


What I have tried:

JObject obj = JObject.Parse(responseFromServer);
           var Items = obj["orders"];
           foreach (var item in Items)
           {
               string orderid = item["orderid"].ToString();
           }
Posted
Updated 5-Jan-23 0:49am
Comments
Richard Deeming 6-Jan-23 7:14am    
It looks like there's a problem with the code which is generating that JSON - the items in the "orders" array have been JSON-encoded twice. You'll need to contact the person responsible for sending you that JSON and get them to fix their serialization code.

To match your expectations, the JSON would need to be:
{"orders": [
  {"orderid": 12477}
]}

1 solution

That JSON isn't an array of arrays: it's a single array.
Run your JSON through an online converter and it'll give you the classes. For C#, that would give:
public class Root
{
    public List<string> orders { get; set; }
}
So obj contains a single class instance that has an orders property which is a collection of strings.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900