简体   繁体   English

使用 Newtonsoft.Json 生成 JSON 时出现问题

[英]Problem generating JSON using Newtonsoft.Json

I am using Newtonsoft.Json to generate following Json result.我正在使用 Newtonsoft.Json 生成以下 Json 结果。 I have generated most of the output except "Attributes" and could not figure it out how to do it.除了“属性”之外,我已经生成了大部分 output 并且无法弄清楚如何去做。 Result is requested by client and cannot be change.结果是客户要求的,不能更改。

Expected result.预期结果。

{
    "TransactionId": "3S76CjmZ3S7",
    "Environment": "sandbox",
    "ApiVersion": "1.0",
    "Error": "",
    "Warning": "",
    "Data_response": {
        "Data_response_data": [{
            "Brand": "Genuine",
            "Number": "E11106660",
            "Description": "DUPLICATE BASE",
            "Images": [{
                    "FileName": "EA10650_STO_FRO_ALL.PNG"
                },
                {
                    "FileName": "EA10660_STO_FRO_ALL.png"
                }
            ],
            "360Images": [{
                "FileName": "EA10660_STO_ALL.zip"
            }],
            "OriginalStrings": "E11106660",
            "Attributes": {
                "Color": "Blue",
                "Weight": "120lbs",
                "Brand": "TRP"
            }
        }]
    }
}

I am using following code to generate json.我正在使用以下代码生成 json。

JObject jObject = JObject.FromObject(new
{
    TransactionId = partRequest.TransactionId,
    Environment = partRequest.Environment,
    ApiVersion = partRequest.ApiVersion,
    Error = "",
    Warning = "",
    Data_response = new
    {
        Data_response_data =
             from p in Wrapper
             orderby p.CompressedNumber
             select new
             {
                 Brand = p.Brand,
                 Number = p.CompressedNumber,
                 Description = p.Description,
                 Images =
                     from d in p.DigitalAssets
                     orderby d.FileName
                     select new
                     {
                         FileName = d.FileName
                     },
                 360Images =
                     from d in p.DigitalAssets360
                     orderby d.FileName
                     select new
                     {
                         FileName = d.FileName
                     },
                 OriginalStrings = p.CompressedNumber,
                 Attributes = ???
                 // I tried this but its throws an exception.
                 // from d in p.Attributes
                 // orderby d.Name
                 // select new
                 // {
                 //  d.Name = d.Value
                 //}
             }
    }
});

Attribute class属性 class

public class Attribute
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
}

Thanks in advance.提前致谢。

From the json you posted it looks like Attributes is an object, not an array.从您发布的 json 看来, Attributes是 object,而不是数组。

The properties don't seem to match your class.这些属性似乎与您的 class 不匹配。 The JSON properties for Attribute are Color , Weight , and Brand . Attribute 的 JSON AttributeColorWeightBrand Your attribute class should look like:您的属性 class 应如下所示:

public class Attribute
{
    public string Color { get; set; }
    public string Weight { get; set; }
    public string Brand { get; set; }
}

Then you could update your code to:然后您可以将代码更新为:

Attributes = new Attribute 
             {
                 Color = p.Attributes.Color,
                 Weight = p.Attributes.Weight,
                 Brand = p.Attributes.Brand,
             }

However, if you're trying to deserialize the JSON it's usually easier to create C# classes from JSON using either json2csharp or Visual Studio: Edit -> Paste Special -> Paste JSON as Classes, as mentioned in the comments. However, if you're trying to deserialize the JSON it's usually easier to create C# classes from JSON using either json2csharp or Visual Studio: Edit -> Paste Special -> Paste JSON as Classes, as mentioned in the comments.

Using the classes below you can deserialize the json:使用以下类,您可以反序列化 json:

var model = JsonConvert.DeserializeObject<Response>(json);

The result from json2csharp is the (slightly edited) classes below: json2csharp 的结果是下面的(稍微编辑过的)类:

public class Image
{
    public string FileName { get; set; }
}

public class _360Images
{
    public string FileName { get; set; }
}

public class Attributes
{
    public string Color { get; set; }
    public string Weight { get; set; }
    public string Brand { get; set; }
}

public class DataResponseData
{
    public string Brand { get; set; }
    public string Number { get; set; }
    public string Description { get; set; }
    public List<Image> Images { get; set; }

    [JsonProperty("360Images")]
    public List<_360Images> _360Images { get; set; }
    public string OriginalStrings { get; set; }
    public Attributes Attributes { get; set; }
}

public class DataResponse
{
    public List<DataResponseData> Data_response_data { get; set; }
}

public class Response
{
    public string TransactionId { get; set; }
    public string Environment { get; set; }
    public string ApiVersion { get; set; }
    public string Error { get; set; }
    public string Warning { get; set; }
    public DataResponse Data_response { get; set; }
}

Attributes need to be transformed into a dictionary.属性需要转换成字典。

Example例子

class Attribute
{
    public string Name { get; set; }
    public string Value { get ; set; }
}

(...)

var attributes = new List<Attribute>
{
    new Attribute() { Name="Color", Value="Blue" },
    new Attribute() { Name= "Weight", Value ="54.4kg" }
};

var d = attributes.ToDictionary(a=> a.Name, a => a.Value);

var json = System.Text.Json.JsonSerializer.Serialize(new { Attributes = d});

Console.WriteLine(json);

Output Output

{"Attributes":{"Color":"Blue","Weight":"54.4kg"}}

In your case it could be something like the following.在您的情况下,它可能类似于以下内容。

Attributes = (from d in p.Attributes
                  orderby d.Name
                 select d).ToDictionary(a=> a.Name, a => a.Value)

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

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