尝试解析这个json文件,似乎卡在空对象和数组上。
{“解析值时遇到意外的字符:[。路径'注释'。”}
{
"id":null,
"phone":null,
"name":" ",
"email":null,
"address":null,
"assignee":null,
"notes":[
],
"created_at":null,
"items":{
"0":{
"minimized":false,
"sku":{
"partner_id":null,
"type_id":0,
"errors":{
}
}
}
}
}
类
public class RootObject
{
public string id { get; set; }
public string phone { get; set; }
public string name { get; set; }
public string email { get; set; }
public string address { get; set; }
public string assignee { get; set; }
public string notes { get; set; }
public string created_at { get; set; }
public Items items { get; set; }
}
public class Items
{
public bool minimized { get; set; }
public Sku sku { get; set; }
}
public class Sku
{
public int partner_id { get; set; }
public int type_id { get; set; }
public Errors errors { get; set; }
}
public class Errors
{
}
问题似乎是处理Notes和Errors属性,我尝试使用以下设置按照其他一些SO帖子但没有任何工作,我不知道如何将其反序列化为对象。
RootObject o = JsonConvert.DeserializeObject<RootObject>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
});
也许有人可以帮助我,因为在我看来JSON.net应该能够处理这些属性吗?
答案 0 :(得分:2)
注意:我在您的json中为您的空值设置了示例值,以便您可以看到它正在运行)
您的班级定义(从http://json2csharp.com/自动生成)需要修改,如下所示。
public class RootObject
{
public string id { get; set; }
public string phone { get; set; }
public string name { get; set; }
public string email { get; set; }
public string address { get; set; }
public string assignee { get; set; }
public List<string> notes { get; set; }
public string created_at { get; set; }
public Dictionary<int,Item> items { get; set; }
}
public class Item
{
public bool minimized { get; set; }
public Sku sku { get; set; }
}
public class Sku
{
public int partner_id { get; set; }
public int type_id { get; set; }
[JsonIgnore]
public object errors { get; set; }
}
由于您已在评论中声明Errors
始终为空,因此我删除了您所拥有的多余Errors
类,没有属性或成员。我还将errors
类中的Sku
成员设置为object
类型,以防将来获得值。最后,我将此errors
属性设置为[JsonIgnore]
,以便json.net将其忽略以进行序列化/反序列化
此外,Items
似乎为Dictionary
,其中int
为关键字,Item
为值。所以我也改变了那里的定义。
这是反序列化并打印出值的代码。
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string json = @"{
""id"":1,
""phone"":""415-000-1234"",
""name"":"" "",
""email"":null,
""address"":null,
""assignee"":null,
""notes"":[
],
""created_at"":null,
""items"":{
""0"":{
""minimized"":false,
""sku"":{
""partner_id"":21,
""type_id"":44,
""errors"":{
}
}
}
}
}";
Console.WriteLine("Deserializing json...");
RootObject o = JsonConvert.DeserializeObject<RootObject>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
});
Console.WriteLine("Success!");
Console.WriteLine("id #: {0}",o.id);
Console.WriteLine("phone #: {0}",o.phone);
foreach (var item in o.items)
{
Console.WriteLine(" Item #: {0}",item.Key);
if (item.Value != null)
{
Console.WriteLine(" SKU: partner_id: {0}",item.Value.sku.partner_id);
Console.WriteLine(" SKU: type_id: {0}",item.Value.sku.type_id);
}
}
}
}
再一次,这是输出。您可以看到正确反序列化的json值。
专业提示:
items
之类的无名属性时,您可能需要检查http://json2csharp.com/生成的内容并将其修改为Dictionary
或{{1} }或NameValuePair
或其他。它是根据具体情况而定。换句话说,对于 99%精心设计的json,您可以使用http://json2csharp.com/“即插即用”,对于剩余的 1%,您必须自定义生成的类,或序列化代码或两者。答案 1 :(得分:0)
问题似乎是在RootObject的自动属性中,您将notes
属性列为string
而不是string[]
- 如果注释确实是一个字符串数组,您的JSON代码段没有显示。
如果您愿意,还应该能够使用List<string>
注释。