1

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"
            }
        }
    ]
}
4
  • 3
    foreach (var lst in settingsNamelst1.Valuestore) Commented Feb 18, 2015 at 15:00
  • 1
    and I am returned with an error. what error? Include it in your question. Commented Feb 18, 2015 at 15:01
  • 1
    The error is caused by Newtonsoft.Json.JsonConvert.DeserializeObject<List<Valuestore>>(json);. Remove that line and use what @EZI suggested instead Commented Feb 18, 2015 at 15:07
  • Perfect. Works fine. Commented Feb 18, 2015 at 15:10

2 Answers 2

2

The easiest thing you can do is to deserialize it dynamically, and acces the objects over dynamic properties.

var json = @"{
                        ""version"": ""2"",
                        ""valuestore"": [
                            {
                                ""name"": ""abcd1"",
                                ""type"": ""string"",
                                ""value"": {
                                    ""value"": ""0002""
                                }
                            },
                            {
                                ""name"": ""abcd2"",
                                ""type"": ""string"",
                                ""value"": {
                                    ""value"": ""001""
                                }
                            }
                        ]
                    }";

        dynamic jsonTemp = JsonConvert.DeserializeObject(json);
        foreach (var i in jsonTemp.valuestore)
        {
            Console.WriteLine("name: {0}, value: {1} \n", i.name, i.value.value);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Selecting this answer. Crisp and clear solution I was looking for.
0

Your settingsNamelst1 property is already deserialized and contains the Valuestore, no need to de-serialize it again:

var settingsNamelst1 = JsonConvert.DeserializeObject<RootObject>(json);
foreach (var item in settingsNamelst1.Valuestore)
{
    listView1.Items.Add(item.name);
}

The reason you're seeing the exception is because your JSON schema doesn't match List<Valuestore>, it matches your RootObject.

4 Comments

Thank you very much for your explanation. How can i access value attribute. For example var settingsNamelst1 = JsonConvert.DeserializeObject<list<value>.(json); Because it give the same error for value
@raj3209812 Come on, don't be so lazy. VS will help you
Ok. Got the hint from Nikola.Lukovic answer.
value.value made the difference. Thanks every one

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.