0

I tray to parse json with using Json.Net but i am new parsing json and i didn't get good result after so many test.

json structure is as below;

 [
   {
      "Coo":{
         "id":"1"
      },
      "Hor":{
         "name":"Poo"
      },
      "Vor":{
         "name":"Soo"
      },
      "Status":"1",
      "Tola":[
         {
            "value":"10",
         },
         {
            "value":"20",
         }
      ],
      "Opt":[

      ]
   },
   {
      "Coo":{
         "id":"2"
      },
      "Hor":{
         "name":"Zoo"
      },
      "Vor":{
         "name":"Koo"
      },
      "Status":"2",
      "Tola":[
         {
            "value":"20",
         },
         {
            "value":"10",
         }
      ],
      "Opt":[

      ]
   },
      {
      "Coo":{
         "id":"3"
      },
      "Hor":{
         "name":"Moo"
      },
      "Vor":{
         "name":"Noo"
      },
      "Status":"1",
      "Tola":[
         {
            "value":"30",
         },
         {
            "value":"20",
         }
      ],
      "Opt":[

      ]
   }
]

My code is as below for parsing.

_JsonString = _JsonString.Trim().Trim('[',']');

JObject _JObject = JObject.Parse(_JsonString);

var _JItems = _JObject.SelectToken(".")
             .Select(s => new
             {
                 _Id = (string)s.SelectToken("Coo.id"),
                 _WhereClause = (string)s.SelectToken("Status")
             })
             .Where(w => w._WhereClause == "1");

foreach (var _JItem in _JItems)
{
    MessageBox.Show(_JItem._Id.ToString());
}

Thank you in advance.

3
  • 1
    What exactly are the issues you have? What is wrong with the result? Commented Feb 14, 2014 at 10:00
  • your code is very hard to read because you use upper case for local variables. Commented Feb 14, 2014 at 10:08
  • @Patrick Hofman result must return 1 and 3 in for block. But I am getting nothing. Commented Feb 14, 2014 at 10:17

1 Answer 1

4

You are using JObject while you should use JArray:

Remove this line:

_JsonString = _JsonString.Trim().Trim('[', ']'); /*removed*/

And change

JObject _JObject = JObject.Parse(_JsonString);

To

JArray _JObject = JArray.Parse(_JsonString);

Full code:

JArray _JObject = JArray.Parse(_JsonString);

var _JItems = _JObject.SelectToken(".")
             .Select(s => new
             {
                 _Id = (string)s.SelectToken("Coo.id"),
                 _WhereClause = (string)s.SelectToken("Status")
             })
             .Where(w => w._WhereClause == "1");

foreach (var _JItem in _JItems)
{
    MessageBox.Show(_JItem._Id.ToString());
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried that but I couldn't implement to my code. Could you give an example?
What is the problem with it? This is your example with two modifications.

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.