1

I'm try to parse some JSON like this:

{
   "results": [
       "MLU413843206",
       "MLU413841098",
       "MLU413806325",
       "MLU413850890",
       "MLU413792303",
       "MLU413843455",
       "MLU413909270",
       "MLU413921617",
       "MLU413921983",
       "MLU413924015",
       "MLU413924085"
   ]
}

All is fine until I try to obtain the values themselves, for example:

 // The JSON is shown above
 var jsonResp = JObject.Parse(json);    
 var items = jsonResp["results"].Children();

I don't know how to obtain the values, each converted to string. Does somebody know how to do this?

4
  • The JSON you're trying to parse is not valid. Between the key and the value there should not be a minus symbol. Commented Dec 19, 2013 at 18:16
  • you should learn json structure first. Means how one json object structure is Commented Dec 19, 2013 at 18:18
  • This looks correct, what exactly isn't working? Commented Dec 19, 2013 at 23:34
  • What I need is obtain the text inside this JSON using JSON.NET , I have obtained this using another techniques with string parsing, etc. But I don't want do it in this way. I want a elegant way using JSON.NET in this case. Commented Dec 21, 2013 at 15:20

1 Answer 1

1

You're halfway there. You can use the Select() method in the System.Linq namespace to project the IEnumerable<JToken> returned from the Children() method into an IEnumerable<string>. From there you can loop over the values using foreach, or put the values into a List<string> using ToList() (or both).

string json = @"
{
    ""results"": [
        ""MLU413843206"",
        ""MLU413841098"",
        ""MLU413806325"",
        ""MLU413850890"",
        ""MLU413792303"",
        ""MLU413843455"",
        ""MLU413909270"",
        ""MLU413921617"",
        ""MLU413921983"",
        ""MLU413924015"",
        ""MLU413924085""
    ]
}";

JObject jsonResp = JObject.Parse(json);
List<string> items = jsonResp["results"].Children()
                                        .Select(t => t.ToString())
                                        .ToList();
foreach (string item in items)
{
    Console.WriteLine(item);
}

Fiddle: https://dotnetfiddle.net/Jcy8Ao

Sign up to request clarification or add additional context in comments.

Comments

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.