I am trying to get the "name" and "value" attribute from the JSON file which i am loading externally.
I tried accessing the list Valuestore and I am returned with an error.
"""Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ExternalJSON.Form1+Valuestore]' because the type requires a JSON array """"
How can i access the list value store and print the name and value in the listview.
My Code
public class Value
{
public string value { get; set; }
}
public class Valuestore
{
public string name { get; set; }
public string type { get; set; }
public Value value { get; set; }
}
public class RootObject
{
public string version { get; set; }
public List<Valuestore> Valuestore { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
var json = File.ReadAllText(".....\\static_settings");
var settingsNamelst1 = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
var settingsNamelst2 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Valuestore>>(json);
foreach (var lst in settingsNamelst2)
{
listView1.Items.Add(lst.name);
}
}
And my JSON file is
{
"version": "2",
"valuestore": [
{
"name": "abcd1",
"type": "string",
"value": {
"value": "0002"
}
},
{
"name": "abcd2",
"type": "string",
"value": {
"value": "001"
}
}
]
}
foreach (var lst in settingsNamelst1.Valuestore)and I am returned with an error.what error? Include it in your question.Newtonsoft.Json.JsonConvert.DeserializeObject<List<Valuestore>>(json);. Remove that line and use what @EZI suggested instead