0

Have some json and parse that whit this code:

dynamic json = JsonConvert.DeserializeObject(response.Content);
dynamic result =json.result;

after this line:

dynamic result =json.result;

have this output:

{
   {
      "321":{
         "online_status":true,
         "basic_info":{
            "status":"Recharged",
            "group_name":"IRN-UV002-M01",
            "isp_name":"Main",
            "creation_date":"2017-09-05 08:19:32",
            "recharge_deposit":0.0,
            "user_id":321,
            "nearest_exp_date":"2018-02-22 10:21:00",
            "credit":20387.775145462037,
            "deposit":0.0,
            "isp_id":0,
            "group_id":72
         },
         "user_repr":"10001168-2100104f4Y8-FTTH",

      }
   }
}


and now want to get user_id from that json,how can i write code for that purpose?thanks.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
behzad
  • 25
  • 7
  • https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c should help. – peeebeee Jan 30 '18 at 08:21
  • did you read [this](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object/%22this%22) post and associated comments? – Moseyza Jan 30 '18 at 08:22
  • See [Deserialize json object into dynamic object using Json.net](https://stackoverflow.com/q/4535840/3744182). – dbc Jan 30 '18 at 08:23
  • Possible duplicate of [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) –  Jan 30 '18 at 08:24
  • Your sample JSON is not well-formed. Upload it to https://jsonlint.com/ and you will see the error `Error: Parse error on line 1: Expecting 'STRING', '}', got '{'` You won't be able to parse that at all with a JSON parser. – dbc Jan 30 '18 at 08:26
  • The basic problem with using `dynamic` for querying parsed JSON is, as you see, that JSON property names can be numeric or otherwise invalid c# identifiers. You need to deserialize or cast to the actual underlying type, in this case `JToken`. See e.g. [Dynamic object property name begins with number](https://stackoverflow.com/a/22733508). That's assuming you actually have well-formed JSON! – dbc Jan 30 '18 at 08:34

3 Answers3

3

The better way would be to deseralize this into a strongly typed object.

But for you you can use the JObject class to do something like the following (note not tested, but you should understand the concept):

dynamic result = JObject.Parse(source);
int id = result.321.basic_info.user_id;
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
2

Another way is to use JObject to hold the string.

var str = "{\"321\":{\"online_status\":true,\"basic_info\":{\"status\":\"Recharged\",\"group_name\":\"IRN-UV002-M01\",\"isp_name\":\"Main\",\"creation_date\":\"2017-09-05 08:19:32\",\"recharge_deposit\":0.0,\"user_id\":321,\"nearest_exp_date\":\"2018-02-22 10:21:00\",\"credit\":20387.775145462037,\"deposit\":0.0,\"isp_id\":0,\"group_id\":72},\"user_repr\":\"10001168-2100104f4Y8-FTTH\"}}";
var obj = JObject.Parse(str);
var userId = obj["321"]["basic_info"]["user_id"].ToString();
Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28
2

You probably want to do something like this:

var yourInstance = JsonConvert.DeserializeObject<YourClass>(responseJson);

For that, you need to define a class YourClass and related sub-classes which have properties matching the values returned in the JSON data, i.e. something like:

public class YourClass {

    public bool online_status { get; set; }

    public BasicInfo basic_info { get; set; }     

    public string user_repr { get; set; }
}

public class BasicInfo {

    public string status { get; set; }
    public string group_name{ get; set; }
    public string isp_name{ get; set; }
    public DateTime creation_date{ get; set; }
    public string group_name{ get; set; }

    // ...etc.

}

With this in place, JsonConvert should be able to understand and parse your data to the correct object.

This is just a rough example, but it should get you on your way.

Kjartan
  • 18,591
  • 15
  • 71
  • 96