I am fetching a string from a JSON file & want to convert it to Key-Value pair.
I am using JSON.NET for the same.
I know I can deserialize a List using:
List<JsonClass> jsonFileComments = JsonConvert.DeserializeObject<List<JsonClass>>(json);
And I can Deserialize a Array using:
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
But as my JSON string contains Lists & Arrays in single file only. The sequence is like:
{
List,
List,
List,
Array,
List,
Array,
List
}
Can you tell me how can I deserialize it using single way.
Other way is I need to separate out the Arrays & work on separately. And again while Serializing I need to work that stuff.
Well one way would be to use dynamic (The type is a static type, but an object of type dynamic bypasses static type checking.)?
dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");
Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);
Hope this code block helps. I'm assuming you have jSon object & you need to parse through it:
Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(yourJsonData);
Newtonsoft.Json.Linq.JArray textArray = (Newtonsoft.Json.Linq.JArray)o["yourArray"];
if (textArray.Count > 0)
{
for (int i = 0; i < textArray.Count; i++)
{//do something}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.