1

I am converting from a model and serializing it to send to a webpage where the JSON is then editable. After editing, I am using AJAX to send the updated JSON to code behind where I need to convert it back to the original model. Upon serializing the model the first time, it maintains a JSON Array followed by JSON Objects. After using JSON.Stringify, the JSON Array is turned into a JSON Object of JSON Objects. I'm dealing with multiple JSON objects that are displayed and edited to be sent to the code behind. The string received is in the format of

{"0":

    {"Id":{},
    "ItemNumber":"1",
    "Person":{
        "PersonNumber": "001",
        "Address": {
            "Street": "Test street 123"
            },
        "email": "[email protected]"
        },
    "Contact": {
        "Name" : "Company A"
        },
    "Quantity" : "12"
    },
"1": etc,
}

I want to loop through each number that acts as a key for the object, however trying to map it to a dictionary or deserializing the object turns the data into {{ "Id":{}, "ItemNumber" : "1", "Person": {etc }} I think I've been looking at the problem so long, the solution is in front of my face and I am over thinking it, I just can't seem to find the right way to get the information in a form that matches the model I am trying to convert it to.

Using JsonConvert to deserialize into a Dictionary<string,Object> gets me very close, but I still end up with the problem of

{
    {
    "Id":{},
    "ItemNumber":"1", 
    etc
    }
}

for a single object. If I can strip the outer layer of brackets for each object, I believe it will be the right format but I do not know how to do this. I feel like I am going about this the wrong way, maybe there is a simpler way to index the Json Object with a for loop. Is there a method belonging to JsonConvert / JObject that can help or make this conversion cleaner, maybe something from JToken?

Here is some code from Code Behind.

[WebMethod]
public static string UpdateItems(string json) //json = {"0":{"Id": etc}}
{
    var result = JsonConvert.DeserializeObject<Dictionary<string, object>>(json); // returns key "0", value { { Format my desired model is in } }
// My goal is to convert it back to the original object like
// JsonConvert.DeserializeObject<Dictionary<string, OriginalModel>> rather than creating a new class
}
2
  • In VS go to your empty class, Edit > Paste Special > Paste JSON As Classes. It'll auto generate classes based on your JSON structure. Commented Mar 28, 2019 at 10:47
  • This works for seeing what the class looks like, however I need to map it to the original model that was converted to JSON. Somewhere along the lines of displaying the JSON, JSON.stringify, and using AJAX to send it back to code behind, it converts some properties into objects that used to be strings. Commented Mar 28, 2019 at 11:06

1 Answer 1

3

You can generate classes from your json with this site or Visual Studio:

public class Id
{
}

public class Address
{
    public string Street { get; set; }
}

public class Person
{
    public string PersonNumber { get; set; }
    public Address Address { get; set; }
    public string email { get; set; }
}

public class Contact
{
    public string Name { get; set; }
}

public class NestedObject
{
    public Id Id { get; set; }
    public string ItemNumber { get; set; }
    public Person Person { get; set; }
    public Contact Contact { get; set; }
    public string Quantity { get; set; }
}

Now you can deserialize your input:

var input = "{\"0\":      {\"Id\":{},     \"ItemNumber\":\"1\",     \"Person\":{         \"PersonNumber\": \"001\",         \"Address\": {             \"Street\": \"Test street 123\"             },         \"email\": \"[email protected]\"         },     \"Contact\": {         \"Name\" : \"Company A\"         },     \"Quantity\" : \"12\"     },  }";
var result = JsonConvert.DeserializeObject<Dictionary<string, NestedObject>>(input);
Sign up to request clarification or add additional context in comments.

6 Comments

Generating the class through Visual Studio works and allows me to map to the new class, however I need to convert it back to the original model it was in, not create a new model.
@Classical31 I don't get what you mean. If you already have some model classes please show them in question. Anyway I don't understand your workflow. Json in question is your input? If so please show expected output
@Classical31 As the answer says - DeserializeObject<T> will take your original model as a type T.
It seems somewhere along the line, the original JSON I display on the webpage is a JSON Array of JSON objects. However, when using AJAX and Stringify, it converts my JSON Array into a JSON Object of JSON Objects. So JSON items that were originally JSON Arrays are now JSON Objects, which makes converting it back to the original model impossible. I do not know how the original JSON form is getting lost in translation.
@Classical31 so you need to create an array of NestedObject instead of dictionary, right?
|

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.