如何将我的json数据反序列化为相关对象..? [英] How to deserialize my json data to relevent object..?

查看:73
本文介绍了如何将我的json数据反序列化为相关对象..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var mockData = [{
               item: {
                   id: "id1",
                   label: "Lorem ipsum dolor 1",
                   checked: false
               },
               children: [{
                   item: {
                       id: "id11",
                       label: "Lorem ipsum dolor 11",
                       checked: false
                   }
               }, {
                   item: {
                       id: 'id12',
                       label: 'Lorem ipsum dolor 12',
                       checked: false
                   }
               }, {
                   item: {
                       id: 'id13',
                       label: 'Lorem ipsum dolor 13',
                       checked: false
                   }
               }]
           }
           ]





我的尝试:



怎么样将我的Json数据反序列化为Relevent Object ...就像Object项目和子项列表一样



公共班级孩子

{

public int id {get;组; }

公共字符串标签{get;组; }

public Boolean Checked {get;组; }

}

公共类项目

{

public int id {get;组; }

公共字符串标签{get;组; }

public Boolean Checked {get;组; }

}



What I have tried:

how to deserialize my Json data to Relevent Object... like list of Object item and children

public class children
{
public int id { get; set; }
public string label { get; set; }
public Boolean Checked { get; set; }
}
public class item
{
public int id { get; set; }
public string label { get; set; }
public Boolean Checked { get; set; }
}

推荐答案

首先,您的JSON无法映射到该对象,因为对象必须已识别出属性作为item,该属性必须是另一个包含这三个属性的对象。 JSON可能很有用,但在JavaScript环境中类型不强(强类型)。可以是这样的样本,

Your JSON cannot be mapped to that object, first of all, because the object "must" have a property identified as "item" and that property must be another object that holds these further three properties. JSON can be helpful, but in JavaScript environment where types are not strong (strongly-typed). A sample can be this,
public class ItemContainer {
   public item item { get; set; }
}



然后为这些创建集合,


Then create the collection for these,

public class JsonContent {
   public JsonContent item { get; set; }
   public List<jsoncontent> children { get; set; }
}
</jsoncontent>



执行此操作将允许您将JSON解析为 JsonContent 对象。例如,像这样,


Doing this would allow you to parse the JSON to a JsonContent object. For example, like this,

// using Newtonsoft.Json;
var obj = JsonConvert.DeserializeObject<list><jsoncontent>>(data);

// I used the List<jsoncontent> because your JSON starts with [] which is an array.
</jsoncontent></jsoncontent></list>



我写了一篇文章,介绍了JSON数据交换格式的基础知识以及如何执行序列化和C#中的反序列化操作。在这里阅读,使用C#从JSON中的零到英雄 [ ^ ]。



其次,我还在同一个帖子上发布了一个非常有趣的问题的答案,如何将随机JSON数据反序列化为C#中的对象 [ ^ ]。当您不知道该怎么做时,我发布了执行JSON反序列化的提示。在那里阅读。


I wrote an article covering the very basics of JSON data-interchange format and how you can perform serialization and deserialization actions in C#. Read it here, From zero to hero in JSON with C#[^].

Secondly, I also posted an answer to a very interesting question on the same thread, How to deserialize a random JSON data to an object in C#[^]. I posted tips to perform the JSON deserialization when you don't know what to do. Read it there.


这篇关于如何将我的json数据反序列化为相关对象..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆