Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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反序列化到一个对象_C#_Json_Wpf_Json.net - Fatal编程技术网

C# 牛顿JSON反序列化到一个对象

C# 牛顿JSON反序列化到一个对象,c#,json,wpf,json.net,C#,Json,Wpf,Json.net,我有一个JSON,如下所示,我试图用以下代码将其反序列化为C对象,但它总是转换为null。如何使用Newton JSON转换它: JSON: C代码: public class RootObject { public Detail detail{get; set;} } public class Detail { public Dictionary<string, Car> CarLst{get; set;} } public class Car { public strin

我有一个JSON,如下所示,我试图用以下代码将其反序列化为C对象,但它总是转换为null。如何使用Newton JSON转换它:

JSON:

C代码:

public class RootObject
{
public Detail detail{get; set;}
}

public class Detail 
{
public Dictionary<string, Car> CarLst{get; set;}
}

public class Car
{
 public string id{get; set;}
 public string text{get; set;}
 public string sub_text{get; set;}
}
要反序列化的C代码:

responseData = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
你忘了一个s

这应该能让它起作用

public class RootObject
{
    public Detail details{get; set;}
}
另一个答案中提到的responseData为null的第一个原因是,与给定的JSON相比,RootObject类中的属性拼写错误

即使修复了这个问题,仍然会有空的详细信息,因为JSON中的下一层不是数组,它们只是额外嵌套的对象。因此,您无法尝试将它们转换为词典

您拥有的JSON也有点奇怪。根据JSON的结构,您需要为JSON中可能返回的每个假定城市指定一个特定属性,这不会使JSON的处理非常动态

现在,要将给定的JSON转换成可用的类数据结构,您需要一些类似于下面的类定义的内容。但是,正如前面提到的,这不是很好的扩展性,并且需要非常繁重的维护

public class RootObject
{
  public Detail details { get; set; }
}

public class Detail
{
  [JsonProperty(PropertyName="MUMBAI")]
  public City Mumbai { get; set; }

  [JsonProperty(PropertyName = "DELHI")]
  public City Delhi { get; set; }

  // Add more of the above property definitions for each individual city that might be inside the returned JSON,
  // which is a very bad design. (Example city of Rohtak)
  [JsonProperty(PropertyName = "ROHTAK")]
  public City Rohtak { get; set; }
}

public class City
{
  public List<Car> car { get; set; }
}

public class Car
{
  public string id { get; set; }
  public string text { get; set; }
  public string sub_text { get; set; }
  public string category_id { get; set; }
  // Might need to add additional possible properties here
}

关于如何使用JsonProperty和其他Newtonsoft.Json功能的更多信息可以在其文档页面中找到

因为在Json的第二级可以有除MUMBAI和DELHI之外的任何字符串值,所以需要将details属性类型更改为Dictionary,其中Cars是一个包含列表属性的类。将类定义更改为以下内容

public class RootObject
{
    public Dictionary<string, Cars> details { get; set; }
}

public class Cars
{
    public List<Car> Car { get; set; }
}

public class Car
{
    public string id { get; set; }
    public string text { get; set; }
    public string sub_text { get; set; }
    public string category_id { get; set; }
}
您可以枚举responseData的嵌套属性以获得汽车的属性。例如,如果我们使用上述json,responseData.details[MUMBAI].Car[0].id将返回ABC,responseData.details[德里].Car[3],category_id将返回delivery


演示:您控制生成的JSON还是来自第三方API?这是第三方API。我不控制它,当您正确使用此JSON时会遇到问题,因为孟买和德里是独立的对象,它们不是给定JSON中的数组,这意味着您必须为每个可能位于JSON中的假定城市拥有一个属性。您确定返回的JSON是正确的吗?
public class RootObject
{
    public Dictionary<string, Cars> details { get; set; }
}

public class Cars
{
    public List<Car> Car { get; set; }
}

public class Car
{
    public string id { get; set; }
    public string text { get; set; }
    public string sub_text { get; set; }
    public string category_id { get; set; }
}
var responseData = JsonConvert.DeserializeObject<RootObject>(jsonResponse);