C # How to parse json data without key name?

I have a json string:

{"fields":[{"type":"none","options":["option1","option2","option3"]}]}

      

I am using JObject to parse json data. I can parse data that has a name, type of type, etc. But how can I parse data that has no name like option1, option2 and option3? Here is my code:

JObject object = JObject.Parse(jsonString);
var type = object["fields"][0]["type"].ToString();

      

but the problem is with the parameters.

+3


source to share


2 answers


A value options

is just an array of values, for example fields

. But each value in it is just a string, not a further map of key / value pairs.

So you can use:

string firstOption = (string) jsonObject["fields"][0]["options"][0];

      



If you want the whole set of options in quality List<string>

, you can use:

var options = jsonObject["fields"][0]["options"]
    .Select(option => (string) option)
    .ToList();

      

+6


source


string jsonString = @"{""fields"":[{""type"":""none"",""options"":[""option1"",""option2"",""option3""]}]}";

var obj = JObject.Parse(jsonString);
var options = obj["fields"][0]["options"].ToList();

      



+1


source







All Articles