0

What is a useful model which could bind a json object with arbitrary keys? Assume your json could look like:

{"@@hello": "address","#world": "address1","name": "address"}

or

{"@@hello": "address","#world": "address1","firstname": "foo", "lastname":"bar"}

or

{"@@hello": "address","#world": "address1","children": [{"name":"foo"},{"name":"bar"}]}

So that means you only know at run time how the json model looks like. My current state is, that it seems the best practice is to send the json object as string to the controller and deserialize the json string it to an object.

  public ActionResult mappingNodes(string model) {
            dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(model);
}
3
  • Could you use Dictionary<string, object>? It's obviously not as nice as having a model, but I think it will be quite difficult to try and map the properties to a model. Commented Aug 5, 2016 at 14:33
  • @Matthew That is possible but I cant see the advantage using Dictionary instead of ` Newtonsoft.Json.JsonConvert.DeserializeObject`. Commented Aug 5, 2016 at 14:40
  • You could also write your own contract resolver to determine how to bind properties: newtonsoft.com/json/help/html/… Commented Aug 5, 2016 at 14:51

1 Answer 1

1
public class Children
    {
        [JsonProperty("name")]
        public string Name { get; set; }
    }
    public class YourClass
    {
        [JsonProperty("@@hello")]
        public string Hello { get; set; }
        [JsonProperty("#world")]
        public string World { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("firstname")]
        public string FirstName { get; set; }
        [JsonProperty("lastname")]
        public string LastName { get; set; }
        [JsonProperty("children")]
        public Children[] Children { get; set; }
    }



        var json1 = "{ '@@hello': 'address','#world': 'address1','name': 'address'}";
        var json2 = "{ '@@hello': 'address','#world': 'address1','name': 'address', 'firstname': 'foo', 'lastname':'bar'}";
        var json3 = "{ '@@hello': 'address','#world': 'address1','name': 'address','children': [{'name':'foo'},{'name':'bar'}]}";


        var model1 = JsonConvert.DeserializeObject<YourClass>(json1);
        var model2 = JsonConvert.DeserializeObject<YourClass>(json2);
        var model3 = JsonConvert.DeserializeObject<YourClass>(json3);
Sign up to request clarification or add additional context in comments.

4 Comments

This does not work with one of my json examples and there for you have to change everytime your model.
It was a smal version of how to do it, now it's the full version. If you need more dynamic maybe you need to use reflection or other technic. Thanks
I cant see in your solution a difference to my suggestion to `serialize the object.
Your controller has to recieve a model (something related your Bussines Logic) and not the json response from some API (Access data). Using this you avoid some parse since the library will do for you. Maybe you can add more info to let me understand what is your case and I'd help you.

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.