0

JSON:

{  
    "o-data":[  
        {  
            "entry 1":"test1",
            "entry 2":"test2",
            "entry 3":"test3",
            "entry 4":"118"
        }
    ]
}

C#:

dynamic dn = dyndata(json);
foreach (KeyValuePair<string, object> kvp in dn)
{
    //I would like to be able to get all the inner data for `o-data` like below...
    string entry1 = "";// test1
    string entry2 = "";// test2
    string entry3 = "";// test3

    //but instead I get `{[o-data, System.Object[]]}`
    string second = kvp.Value.ToString(); // gives my 'System.Object[]'
}

public dynamic dyndata(string json)
{
    dynamic dyn1 = new System.Dynamic.ExpandoObject();
    try
    {
        var serializer = new JavaScriptSerializer();
        var result = serializer.DeserializeObject(json);
        return result);
    }
    catch { return Tuple.Create(dDummy, false); }
}

How can I the KVP of the inner list for 'o-data'.

Currently it only loop once because of o-data. I would like to get the inner list of o-data and loop through that.

1 Answer 1

2

Some castings are required:
1) cast the kvp.Value to object[] because "o-data" contains an array
2) cast the first item of this array to IDictionary<string, object>

dynamic dn = dyndata(json);
foreach (KeyValuePair<string, object> kvp in dn)
{
    var array = ((object[])kvp.Value);
    var dictionary = (IDictionary<string, object>) array[0];
    var entry1 = dictionary["entry 1"];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you... it's weird, locally your code works but i had to revert back to my code to work correctly when the site is hosted in IIS... weird weird weird! Thanks for the help. +1 and accepted.

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.