Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# json使用newtonsoft序列化列表_C#_Json_Serialization_Json.net - Fatal编程技术网

C# json使用newtonsoft序列化列表

C# json使用newtonsoft序列化列表,c#,json,serialization,json.net,C#,Json,Serialization,Json.net,我想使用newtonsoft json Serializer来创建json。 我是一个全新的建筑清单和收藏品,所以我会向你们寻求帮助。 创建一个简单的json就可以了。 我必须用属性列表建立一个属性列表 Maybee还有另一个图书馆,让这更容易 我需要这个json: { "objectTypeId": 545, "attributes": [{ "objectTypeAttributeId": 222, "objectAttributeValues

我想使用newtonsoft json Serializer来创建json。 我是一个全新的建筑清单和收藏品,所以我会向你们寻求帮助。 创建一个简单的json就可以了。 我必须用属性列表建立一个属性列表

Maybee还有另一个图书馆,让这更容易

我需要这个json:

{
    "objectTypeId": 545,
    "attributes": [{
        "objectTypeAttributeId": 222,
        "objectAttributeValues": [{
            "value": "Kommentar"
        }]
    }]
}
因此,我开始

public class Controller
{
        public int objectTypeId { get; set; }
        public IList<string> attributes { get; set; }
}

Controller controllerjson = new Controller
{
  objectTypeId = 545,
  // How to add the attributes with each values ?
}
公共类控制器
{
public int objectTypeId{get;set;}
公共IList属性{get;set;}
}
控制器controllerjson=新控制器
{
objectTypeId=545,
//如何添加每个值的属性?
}
您的c#类模型无法与JSON数据匹配

有两种方法可以轻松创建模型

  • 您可以在VisualStudio中使用WebEssentials,使用Edit>Paste special>Paste JSON作为类,您可以更容易地了解JSON和模型之间的关系

  • 如果您不能使用WebEssentials,您可以使用在线JSON来为类建模

  • 您可以尝试使用这些模型来携带JSON格式

    public class ObjectAttributeValue
    {
        public string value { get; set; }
    }
    
    public class Attribute
    {
        public int objectTypeAttributeId { get; set; }
        public List<ObjectAttributeValue> objectAttributeValues { get; set; }
    }
    
    public class RootObject
    {
        public int objectTypeId { get; set; }
        public List<Attribute> attributes { get; set; }
    }
    
    公共类ObjectAttributeValue
    {
    公共字符串值{get;set;}
    }
    公共类属性
    {
    public int objectTypeAttributeId{get;set;}
    公共列表ObjectAttributeValue{get;set;}
    }
    公共类根对象
    {
    public int objectTypeId{get;set;}
    公开名单

    RootObject controllerjson = new RootObject()
    {
        objectTypeId = 545,
        attributes = new List<Attribute>() {
            new Attribute() {
                objectTypeAttributeId = 222,
                objectAttributeValues = new List<ObjectAttributeValue>() {
                    new ObjectAttributeValue() { value= "Kommentar" }
                }
            }
        }
    };
    
    string jsonStr = JsonConvert.SerializeObject(controllerjson);