简体   繁体   English

C#如何将复杂的JSON Object的子对象转换为模型

[英]C# How to convert complex JSON Object's child objects to a model

I am converting from a model and serializing it to send to a webpage where the JSON is then editable. 我正在从模型转换并将其序列化以发送到可以编辑JSON的网页。 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. 编辑后,我使用AJAX将更新的JSON发送到我需要将其转换回原始模型的代码后面。 Upon serializing the model the first time, it maintains a JSON Array followed by JSON Objects. 在第一次序列化模型时,它维护一个JSON数组,后跟JSON对象。 After using JSON.Stringify, the JSON Array is turned into a JSON Object of JSON Objects. 使用JSON.Stringify后,JSON数组将转换为JSON对象的JSON对象。 I'm dealing with multiple JSON objects that are displayed and edited to be sent to the code behind. 我正在处理多个JSON对象,这些对象被显示和编辑以发送到后面的代码。 The string received is in the format of 收到的字符串格式为

{"0":

    {"Id":{},
    "ItemNumber":"1",
    "Person":{
        "PersonNumber": "001",
        "Address": {
            "Street": "Test street 123"
            },
        "email": "test@email.com"
        },
    "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. 我想循环遍历每个作为对象键的数字,但是尝试将其映射到字典或反序列化对象会将数据转换为{{ "Id":{}, "ItemNumber" : "1", "Person": {etc }}我想我一直在看这个问题这么长时间,解决方案就在我的面前,我在思考它,我似乎无法找到正确的方法来获取信息以与我试图将其转换为的模型匹配的形式。

Using JsonConvert to deserialize into a Dictionary<string,Object> gets me very close, but I still end up with the problem of 使用JsonConvert反序列化为Dictionary<string,Object>让我非常接近,但我仍然最终遇到了问题

{
    {
    "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. 我觉得我这样做的方式不对,也许有一种更简单的方法可以用for循环索引Json对象。 Is there a method belonging to JsonConvert / JObject that can help or make this conversion cleaner, maybe something from JToken ? 是否有一个属于JsonConvert / JObject的方法可以帮助或使这个转换更清洁,也许来自JToken东西?

Here is some code from Code Behind. 以下是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
}

You can generate classes from your json with this site or Visual Studio: 您可以使用此站点或Visual Studio从json生成类:

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\": \"test@email.com\"         },     \"Contact\": {         \"Name\" : \"Company A\"         },     \"Quantity\" : \"12\"     },  }";
var result = JsonConvert.DeserializeObject<Dictionary<string, NestedObject>>(input);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM