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

C# Json反序列化问题

C# Json反序列化问题,c#,json,C#,Json,正在尝试从web api反序列化json。以下是传入的json: {"0":{"productname":"France and the French", "imageurl":"http://img.rakuten.com/PIC/32994457/0/1/250/32994457.jpg", "producturl":"http://www.theurl.com/rd.aspx?3d", "price":"19.51", "currency":"USD", "saleprice":"", "

正在尝试从web api反序列化json。以下是传入的json:

{"0":{"productname":"France and the French",
"imageurl":"http://img.rakuten.com/PIC/32994457/0/1/250/32994457.jpg",
"producturl":"http://www.theurl.com/rd.aspx?3d",
"price":"19.51",
"currency":"USD",
"saleprice":"",
"storename":"Buy.com (dba Rakuten.com Shopping)"}
}
这是我的数据类:

public class Product
{
    [JsonProperty("productname")]
    public string productname { get; set; }

    [JsonProperty("imageurl")]
    public string imageurl { get; set; }

    [JsonProperty("producturl")]
    public string producturl { get; set; }

    [JsonProperty("price")]
    public string price { get; set; }

    [JsonProperty("currency")]
    public string currency { get; set; }

    [JsonProperty("saleprice")]
    public string saleprice { get; set; }

    [JsonProperty("storename")]
    public string storename { get; set; }
}
代码如下:

string json = upcclient.GetProductJSON(upc, accessToken);
Product myproduct = JsonConvert.DeserializeObject<Product>(json);
string json=upcclient.GetProductJSON(upc,accessToken);
Product myproduct=JsonConvert.DeserializeObject(json);
应该很直截了当。json正在被填充,但无论我尝试什么——产品作为主数据类中的产品列表、产品数组等,myproduct都不会被填充


提前谢谢你的帮助。这东西通常没这么硬。我已经做了50次左右了,但这一次对我来说越来越重要了

在您发布的示例JSON中,
Product
是根对象的
0
字段中包含的对象,并且根对象似乎没有使用JSON数组语法。不幸的是,如果您希望得到任意数量的结果,那么很难优雅地映射到C#类。如果您知道只会得到一个产品,只需添加一个根类:

public class ProductWrapper
{
  [JsonProperty("0")]
  public Product Product
  {
    get;
    set;
  }
}
并且做:

string json = upcclient.GetProductJSON(upc, accessToken);
Product myproduct = JsonConvert.DeserializeObject<ProductWrapper>(json).Product;
string json=upcclient.GetProductJSON(upc,accessToken);
Product myproduct=JsonConvert.DeserializeObject(json).Product;

不过,我怀疑这个API是为了潜在地返回多个产品而设计的,所以这可能不是您想要的。如果这是一个您可以控制的API,那么可能值得请求JSON实际返回产品的数组,而不是现在返回的对象。

键可能在开头,带有
{“0”:
你似乎知道你自己问题的答案..但你考虑太多了。
产品作为主数据类中的产品列表,产品数组
-正确。这是一个数组。将其反序列化为一个数组。从技术上讲,JSON数组应该在方括号{“0”:[{data here}]}。在本例中,它不是。我从位于花括号内的JSON数组的提供程序处获得它,并且只有一项。我刚才尝试了以下操作,但没有执行列表p=(List)JsonConvert.DeserializeObject(JSON,typeof(List));它们就在上面,我想得太多了。您的解决方案工作正常(假设产品已更改为ProductWrapper)。谢谢。我刚听说JSON一直都是错误的。