I am trying to parse through an json array and get the values from it but facing some issues.
I have my json that I get as:
[{
"ID1":"1",
"ID2":"2",
"ID3":"3",
"ID4":"4"
},
{
"ID1":"5",
"ID2":"6",
"ID3":"7",
"ID4":"8"
}]
The key ID1, ID2 etc are not fixed. For ex I can also have my json as:
[{
"ID1":"1",
"ID2":"2"
},
{
"ID1":"5",
"ID2":"6"
}]
Or there can be more keys also, ie its dynamic so I cant create a model and map my json to it.
I was trying to deserialize that and get the data by using the following code.:
public IActionResult GetData(string data)
{
//here data is my json
List<string> myList = JsonConvert.DeserializeObject<List<string>>(data);
foreach (string s in myList)
{
//get values here
}
return Json("");
}
But above gives me error when deserializing as:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while >parsing value: {. Path '', line 1, position 2.'
So I wanted to know how what is the correct method to loop through this json and get key values from it.
Dictionary<string, string>as your objectDictionary<string, string>