How do Deserialize objects/arrays that have variable name

Dec 16, 2009 at 1:06 PM
Edited Dec 16, 2009 at 1:14 PM

This JSON project is great, but i can't figure out how i can Deserialize objects/arrays where i don't know the name from.

Like the following JSON

                  "value":"36 x 25 cm",
                  "value":"Three Worlds",
This is just a litle example of arrays that i need to deserialize where i don't know the names from
I have tried hunderd different things (i think) but without result.
The best result a got from the following:
    public class LocalViewResponseGraph
        public Dictionary<string, object> subject;
Now i get two KVP with keys: prefixes and
AGGHHHH, and now the objects are empty in .Net. Any suggestions???
PS: the field is a variable name, the next time i get a JSON response its
a different name :-(
Dec 17, 2009 at 7:02 PM

Look at what your JSON is... it is a nested structure of dictionaries and arrays.

Because your JSON is quite complex you might want to use LINQ to JSON to manually construct a simpler class representation of the data in .NET

Dec 18, 2009 at 12:40 PM
Edited Dec 23, 2009 at 5:48 AM

Yes, i have worked it out!

Thanks for the tip, and here some part of my solution, maybe it will help someone on the way whom wants to use LINQ on this.

strJson = File.ReadAllText("test.js");

            // Parse string to Object
            JObject jsonObject = JObject.Parse(strJson);
            // Gets basic info
            resultObject.Uri = (string)jsonObject["about"]["uri"];
            resultObject.Label = (string)jsonObject["about"]["label"];
            resultObject.Description = (string)jsonObject["about"]["description"];
            resultObject.ImageUrl = (string)jsonObject["about"]["image"];
            resultObject.Relations = new List<LocalViewObjectRelation>();

            // Get all Display Objects (how to display URI's)
            var displayObjects = from g in jsonObject["display"].Children()
                                    new { Uri = ((JProperty)g).Name,
                                          Value = (string)((JProperty)g).Value["label"][0]["value"],
                                          Type = (string)((JProperty)g).Value["label"][0]["type"]

            // Get all graphs (relations)
            var graphs = from g in jsonObject["graph"]["subject"][resultObject.Uri].Children()
                         select new { graph = g };

            // Parse each graph in a relationObject
            foreach (var g in graphs)
                // new relation
                LocalViewObjectRelation relation = new LocalViewObjectRelation();

                if (g.graph is JProperty)
                    // parse to redable object
                    JProperty relationUriObject = g.graph as JProperty;

                    // Basis info
                    relation.Uri = relationUriObject.Name; // URI
                    relation.Value = (string)relationUriObject.Value[0]["value"]; // Value of URI
                    relation.ValueType = (string)relationUriObject.Value[0]["type"]; // Type of Value

                    // Look for display information
                    var displayValue = displayObjects.SingleOrDefault(a => a.Uri == relation.Uri);
                    relation.DisplayValue = (displayValue == null) ? null : displayValue.Value;

                    var displayUri = displayObjects.SingleOrDefault(a => a.Uri == relation.Value);
                    relation.DisplayUri = (displayUri == null) ? null : displayUri.Value;

                // add relation to resultObject
                if(relation.Uri != null && relation.Value != null)

PS: this is working with a javascript bigger then the sample above, the same but more objects