0

I am not much of a C# Programmer so I am fairly new to this, I would like to parse the sample JSON below, I have been using the code:

    WebClient client = new WebClient();
    string getString = client.DownloadString(url);

    dynamic j = JsonConvert.DeserializeObject(getString);
    var k = j.rgDescriptions;

    dynamic m = JsonConvert.DeserializeObject(k);
    foreach (var c in m.descriptions)
          {
                Console.WriteLine(c);
          }

I get error in deserialize of k, I am not sure if I am at the right path though. How do I get the "classid" w/o getting the value of their parent, because it is dynamic and not named, it is a Unique ID.

{
    "success": true,
    "rgDescriptions": {
        "671219543": {
            "id": "671219543",
            "classid": "253033065",
            "instanceid": "93973071",
            "amount": "1",
            "pos": 274
        },
        "707030894": {
            "id": "707030894",
            "classid": "166354998",
            "instanceid": "0",
            "amount": "1",
            "pos": 277
        },

Update:

I used this code:

 WebClient client = new WebClient();
            string getString = client.DownloadString(url);


            var jo = JObject.Parse(getString);
            var data = (JObject)jo["rgDescriptions"];
            foreach (var item in data)
            {
                Console.WriteLine("{0}: {1}", item.Key, item.Value);
            }

I could get what I wanted now, but I need to parse each value. Is there a better way?

2
  • Why are you trying to deserialize a second time? You've deserialized your JSON once to a dynamic .NET object, you don't need to deserialize again. Commented Jun 5, 2014 at 0:40
  • @MattBurland see my updates, is that the correct way? Commented Jun 5, 2014 at 0:41

1 Answer 1

1

You could use JSON.NET and JSONPath to query the incoming JSON, examples here and here

The code below extracts every classid for each object in rgDescriptions

//...
WebClient client = new WebClient();
string getString = client.DownloadString(url);

var obj = JObject.Parse(getString);
var classIds = obj.SelectTokens("$.rgDescriptions.*.classid").Select(x => x.Value<string>()).ToList();  //[253033065,166354998]

//Class ID Where...
var idToSearch = "671219543";
var classId = obj.SelectToken("$.rgDescriptions['" + idToSearch + "']..classid").Value<string>();
//...
Sign up to request clarification or add additional context in comments.

6 Comments

is there a way to find a certain id and get its classid, that way I dont have to parse it 1 at a time?
Yep, I've added an example to my answer. Assuming the name of the object is the same as the Id for that particular query.
Thanks! Works great, 1 last thing.. what if I would like to get instanceid too, do i just do it like this: var classId = obj.SelectToken("$.rgDescriptions['" + idToSearch + "']..instanceid").Value<string>();
Yep, that should work for you. I'd stress if you plan to use the code above, that you read up on JSONPath. If you run into an issue with the code, you should be able to debug it yourself.
When I tried it in my test project console it was working, when I transferred it to where I need to use it, this error showed as exception: Unexpected character while parsing path indexer .. I am unsure now why was this isnt working on my main proj.
|

Your Answer

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.