Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

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

1 Answer 1

up vote 3 down vote accepted

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());
}
share|improve this answer
    
I have tried that but I couldn't implement to my code. Could you give an example? –  Kerberos Feb 14 at 11:36
    
What is the problem with it? This is your example with two modifications. –  Patrick Hofman Feb 14 at 11:44
    
@Kerberos: added full code. –  Patrick Hofman Feb 14 at 11:45
    
Thank you very much you saved my time. –  Kerberos Feb 14 at 12:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.