Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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

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中的嵌套类?_C#_Json_Deserialization - Fatal编程技术网

C# 将JSON字符串反序列化为C中的嵌套类?

C# 将JSON字符串反序列化为C中的嵌套类?,c#,json,deserialization,C#,Json,Deserialization,我从HTTP请求收到一个JSON字符串,如下所示: [ { "size":590, "location":"California", "report":{ "Bob":[ null, "12.0.250.0", "20130228" ], "Mary":[ null, "2013-02-28.01", "20130228" ], "J

我从HTTP请求收到一个JSON字符串,如下所示:

[
   {
  "size":590,
  "location":"California",
  "report":{
     "Bob":[
        null,
        "12.0.250.0",
        "20130228"
     ],
     "Mary":[
        null,
        "2013-02-28.01",
        "20130228"
     ],
     "John":[
        null,
        "12.00",
        "59123805"
     ]
  }
},
{
  "size":12348,
  "location":"Florida",
  "report":{
     "Misty":[
        null,
        "65492.592",
        "89216753"
     ],
     "Billy":[
        null,
        "789208.w9",
        "65320880"
     ],
     "John":[
        null,
        "89.8056",
        "75920889"
     ]
   }
 }
]
[DataContract]
public class DeserializedObject
{
    [DataMember(Name = "size")]
    public UInt64 Size { get; set; }

    [DataMember(Name = "location")]
    public string Location { get; set; }

    [DataMember(Name = "report")]
    public Dictionary<string, string[]> Report { get; set; } 
}
StreamReader reader = new StreamReader(stream);
List<DeserializedObject> instance = JsonConvert.DeserializeObject<List<DeserializedObject>>(reader.ReadToEnd());
我试图反序列化到的类的结构如下:

[DataContract]
public class DeserializedObject
{
    [DataMember(Name = "size")]
    public UInt64 Size { get; set; }

    [DataMember(Name = "location")]
    public string Location { get; set; }

    [DataMember(Name = "report")]
    public Dictionary<string, ReportData> Report { get; set; } 
}

[CollectionDataContract]
public class ReportData : List<object>
{
    public string a
    {
        get { return (string)this[0]; }
        set { this[0] = (string)value; }
    }

    public string b
    {
        get { return (string)this[1]; }
        set { this[1] = (string)value; }
    }

    public string c
    {
        get { return (string)this[2]; }
        set { this[2] = (string)value; }
    }
}
但是,当我尝试反序列化它时,报告总是空的

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<DeserializedObject>));
List<DeserializedObject> list = serializer.ReadObject(stream) as List<DeserializedObject>;

反序列化JSON响应的报表部分的正确方法是什么?

问题在于您的报表类设置不正确。json.NET正在查找键a、b和c。此外,字典中的值是字符串数组,而不是对象。ReportData是一个对象。这种不匹配是导致错误的原因

我认为应该从项目中删除report类型的字典和ReportData。如果我要这样做,我会使用下面的类,DataContract的东西实际上是不必要的

  public class DeserializedObject
  {
      public UInt64 size { get; set; }

       public string location { get; set; }

       public Dictionary<string, string[]> report { get; set; } 
   }

我能够使用JSON.NET成功地反序列化JSON字符串。我用来反序列化字符串的类如下所示:

[
   {
  "size":590,
  "location":"California",
  "report":{
     "Bob":[
        null,
        "12.0.250.0",
        "20130228"
     ],
     "Mary":[
        null,
        "2013-02-28.01",
        "20130228"
     ],
     "John":[
        null,
        "12.00",
        "59123805"
     ]
  }
},
{
  "size":12348,
  "location":"Florida",
  "report":{
     "Misty":[
        null,
        "65492.592",
        "89216753"
     ],
     "Billy":[
        null,
        "789208.w9",
        "65320880"
     ],
     "John":[
        null,
        "89.8056",
        "75920889"
     ]
   }
 }
]
[DataContract]
public class DeserializedObject
{
    [DataMember(Name = "size")]
    public UInt64 Size { get; set; }

    [DataMember(Name = "location")]
    public string Location { get; set; }

    [DataMember(Name = "report")]
    public Dictionary<string, string[]> Report { get; set; } 
}
StreamReader reader = new StreamReader(stream);
List<DeserializedObject> instance = JsonConvert.DeserializeObject<List<DeserializedObject>>(reader.ReadToEnd());
用于反序列化对象的代码如下所示:

[
   {
  "size":590,
  "location":"California",
  "report":{
     "Bob":[
        null,
        "12.0.250.0",
        "20130228"
     ],
     "Mary":[
        null,
        "2013-02-28.01",
        "20130228"
     ],
     "John":[
        null,
        "12.00",
        "59123805"
     ]
  }
},
{
  "size":12348,
  "location":"Florida",
  "report":{
     "Misty":[
        null,
        "65492.592",
        "89216753"
     ],
     "Billy":[
        null,
        "789208.w9",
        "65320880"
     ],
     "John":[
        null,
        "89.8056",
        "75920889"
     ]
   }
 }
]
[DataContract]
public class DeserializedObject
{
    [DataMember(Name = "size")]
    public UInt64 Size { get; set; }

    [DataMember(Name = "location")]
    public string Location { get; set; }

    [DataMember(Name = "report")]
    public Dictionary<string, string[]> Report { get; set; } 
}
StreamReader reader = new StreamReader(stream);
List<DeserializedObject> instance = JsonConvert.DeserializeObject<List<DeserializedObject>>(reader.ReadToEnd());

它是空字典对象还是纯空字典对象?它是空字典对象。我尝试对报表使用字典,但它仍然返回空的非空字典对象。我还试着用字典,但也没用。