Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.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#_.net_Json_Serialization - Fatal编程技术网

C# 反序列化Json字符串类型未知

C# 反序列化Json字符串类型未知,c#,.net,json,serialization,C#,.net,Json,Serialization,我的建议如下: { "request" : { "command" : "series", "series_id" : "PET.WCRSTUS1.W" }, "series" : [ { "series_id" : "PET.WCRSTUS1.W", "name" : "U.S. Ending Stocks of Crude Oil, Weekly", "units" : "Thousand Barrels", "f" : "W", "u

我的建议如下:

   {
 "request" :
 {
  "command" : "series",
  "series_id" : "PET.WCRSTUS1.W"
 },
 "series" : [
  {
   "series_id" : "PET.WCRSTUS1.W",
   "name" : "U.S. Ending Stocks of Crude Oil, Weekly",
   "units" : "Thousand Barrels",
   "f" : "W",
   "unitsshort" : "Mbbl",
   "description" : "U.S. Ending Stocks of Crude Oil",
   "copyright" : "None",
   "source" : "EIA, U.S. Energy Information Administration",
   "iso3166" : "USA",
   "geography" : "USA",
   "start" : "19820820",
   "end" : "20160715",
   "updated" : "2016-07-20T13:18:37-0400",
   "data" : [["20160715", 1214561], 
   ["20160708", 1216904], 
   ["20160701", 1219452], 
   ["20160624", 1221677], 
   ["20160617", 1225730], 
   ["20160610", 1226647], 
   ["20160603", 1227580]]
  }
 ]
}
public class Request
{
    public string command { get; set; }
    public string series_id { get; set; }
}

public class Series
{
    public string series_id { get; set; }
    public string name { get; set; }
    public string units { get; set; }
    public string f { get; set; }
    public string unitsshort { get; set; }
    public string description { get; set; }
    public string copyright { get; set; }
    public string source { get; set; }
    public string iso3166 { get; set; }
    public string geography { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public string updated { get; set; }
    public List<List<Object>> data { get; set; }
}


public class RootObject
{
    public Request request { get; set; }
    public List<Series> series { get; set; }
}
public class Series
{
    public string series_id { get; set; }
    public string name { get; set; }
    public string units { get; set; }
    public string f { get; set; }
    public string unitsshort { get; set; }
    public string description { get; set; }
    public string copyright { get; set; }
    public string source { get; set; }
    public string iso3166 { get; set; }
    public string geography { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public string updated { get; set; }
    public List<DataItem> data { get; set; }
}
我使用了一个在线JSON转换器来生成C#,如下所示:

   {
 "request" :
 {
  "command" : "series",
  "series_id" : "PET.WCRSTUS1.W"
 },
 "series" : [
  {
   "series_id" : "PET.WCRSTUS1.W",
   "name" : "U.S. Ending Stocks of Crude Oil, Weekly",
   "units" : "Thousand Barrels",
   "f" : "W",
   "unitsshort" : "Mbbl",
   "description" : "U.S. Ending Stocks of Crude Oil",
   "copyright" : "None",
   "source" : "EIA, U.S. Energy Information Administration",
   "iso3166" : "USA",
   "geography" : "USA",
   "start" : "19820820",
   "end" : "20160715",
   "updated" : "2016-07-20T13:18:37-0400",
   "data" : [["20160715", 1214561], 
   ["20160708", 1216904], 
   ["20160701", 1219452], 
   ["20160624", 1221677], 
   ["20160617", 1225730], 
   ["20160610", 1226647], 
   ["20160603", 1227580]]
  }
 ]
}
public class Request
{
    public string command { get; set; }
    public string series_id { get; set; }
}

public class Series
{
    public string series_id { get; set; }
    public string name { get; set; }
    public string units { get; set; }
    public string f { get; set; }
    public string unitsshort { get; set; }
    public string description { get; set; }
    public string copyright { get; set; }
    public string source { get; set; }
    public string iso3166 { get; set; }
    public string geography { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public string updated { get; set; }
    public List<List<Object>> data { get; set; }
}


public class RootObject
{
    public Request request { get; set; }
    public List<Series> series { get; set; }
}
public class Series
{
    public string series_id { get; set; }
    public string name { get; set; }
    public string units { get; set; }
    public string f { get; set; }
    public string unitsshort { get; set; }
    public string description { get; set; }
    public string copyright { get; set; }
    public string source { get; set; }
    public string iso3166 { get; set; }
    public string geography { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public string updated { get; set; }
    public List<DataItem> data { get; set; }
}
然后修改系列类,如下所示:

   {
 "request" :
 {
  "command" : "series",
  "series_id" : "PET.WCRSTUS1.W"
 },
 "series" : [
  {
   "series_id" : "PET.WCRSTUS1.W",
   "name" : "U.S. Ending Stocks of Crude Oil, Weekly",
   "units" : "Thousand Barrels",
   "f" : "W",
   "unitsshort" : "Mbbl",
   "description" : "U.S. Ending Stocks of Crude Oil",
   "copyright" : "None",
   "source" : "EIA, U.S. Energy Information Administration",
   "iso3166" : "USA",
   "geography" : "USA",
   "start" : "19820820",
   "end" : "20160715",
   "updated" : "2016-07-20T13:18:37-0400",
   "data" : [["20160715", 1214561], 
   ["20160708", 1216904], 
   ["20160701", 1219452], 
   ["20160624", 1221677], 
   ["20160617", 1225730], 
   ["20160610", 1226647], 
   ["20160603", 1227580]]
  }
 ]
}
public class Request
{
    public string command { get; set; }
    public string series_id { get; set; }
}

public class Series
{
    public string series_id { get; set; }
    public string name { get; set; }
    public string units { get; set; }
    public string f { get; set; }
    public string unitsshort { get; set; }
    public string description { get; set; }
    public string copyright { get; set; }
    public string source { get; set; }
    public string iso3166 { get; set; }
    public string geography { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public string updated { get; set; }
    public List<List<Object>> data { get; set; }
}


public class RootObject
{
    public Request request { get; set; }
    public List<Series> series { get; set; }
}
public class Series
{
    public string series_id { get; set; }
    public string name { get; set; }
    public string units { get; set; }
    public string f { get; set; }
    public string unitsshort { get; set; }
    public string description { get; set; }
    public string copyright { get; set; }
    public string source { get; set; }
    public string iso3166 { get; set; }
    public string geography { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public string updated { get; set; }
    public List<DataItem> data { get; set; }
}
公共类系列
{
公共字符串系列_id{get;set;}
公共字符串名称{get;set;}
公共字符串单位{get;set;}
公共字符串f{get;set;}
公共字符串unitsshort{get;set;}
公共字符串说明{get;set;}
公共字符串版权{get;set;}
公共字符串源{get;set;}
公共字符串iso3166{get;set;}
公共字符串地理{get;set;}
公共字符串开始{get;set;}
公共字符串结束{get;set;}
公共字符串已更新{get;set;}
公共列表数据{get;set;}
}
当我这样做时,我在data属性中获得了正确的项数,但每个单独的DataItem都有默认值(空字符串和0表示值)。请有人建议如何实现这一点

我的反序列化是使用System.Runtime.Serialization(而不是Json.Net)。为此,可以认为反序列化例程超出了我的控制范围。不确定这是否相关,但请参阅下面的代码片段:

    public static T FromJsonString<T>(string json, Type[] extraTypes = null)
    {
        T result;
        var serializer = new DataContractJsonSerializer(typeof(T), extraTypes ?? new Type[] { });

        using (var stream = new MemoryStream())
        {
            using (var writer = new StreamWriter(stream))
            {
                writer.Write(json);
                writer.Flush();
                stream.Position = 0;
                result = (T) serializer.ReadObject(stream);
            }
        }

        return result;
    }
public static T FromJsonString(字符串json,类型[]extraTypes=null)
{
T结果;
var serializer=newdatacontractjsonserializer(typeof(T),extraTypes??new Type[]{});
使用(var stream=new MemoryStream())
{
使用(var writer=新的StreamWriter(流))
{
Write.Write(json);
writer.Flush();
流位置=0;
结果=(T)serializer.ReadObject(流);
}
}
返回结果;
}
因此,总结如下:

  • 如何将要反序列化到DataItem类的数据对象列表?JSON中没有表示该类型
我正在使用.NET4.0


希望这是清楚的-如果没有,请发表评论。

如果您特别关注json

     "data" : [["20160715", 1214561], 
   ["20160708", 1216904], 
   ["20160701", 1219452], 
   ["20160624", 1221677], 
   ["20160617", 1225730], 
   ["20160610", 1226647], 
   ["20160603", 1227580]]
数据是数组的数组,因此您使用的在线生成器已将json正确地转换为
公共列表数据{get;set;}


但是,您可以稍后将其映射到
公共列表数据{get;set;}
,只需迭代
公共列表数据{get;set;}
,并为每个内部列表创建一个
数据项

在无法更改传入json的情况下,我认为您必须同时拥有两个属性:

public List<List<Object>> Data { get; set; }
public List<DataItem> DataMapped { get; set; }

此处没有检查期望值,因此您可能需要添加该值-例如,内部列表肯定包含2个对象等等。

您可以更改json吗?我认为,如果不更改json,您想要的东西是不可能的。json是通过外部服务返回的。我想我可以在反序列化之前操作字符串,但它是一个巨大的字符串(有数千个DataItem,为了清晰起见,我缩短了示例)。简短的回答-不是真的!我曾希望在Series类中有一种通过属性的方法。我不会操纵字符串-你会自找麻烦的!见我的回答谢谢@preethingh。我知道这一点,但我希望将此作为反序列化的一部分,前提是反序列化程序必须已经在迭代。正如您所说,您的序列化程序超出了范围,因此正在修改FromJsonString方法,插入自定义序列化程序的时间已所剩无几,但是,如果您的应用程序是Web API应用程序,则可以插入MediaTypeFormatter。但我的观点是,如果传入的数据是一个数组,那么为什么要转换对象数组呢?在本例中,一天结束时的数据项也是一个数据元组。谢谢@Rick。我会投票,但不会接受你的答案(我仍然希望有一种基于属性的方法来实现这一点!)。我不希望再次遍历所有数据。不用担心-可能会有,但如果有,我就无能为力-我也很想看到。您可以始终使您的数据项具有
列表内容
属性,然后使您的日期和值属性返回内容[0]和内容[1]。我已经更新了我的答案以显示这种方法啊-是的,我想数据类型不会正确匹配!值得一试