JSON 问题

.Net技术 码拜 8年前 (2016-05-17) 904次浏览
本人得到了JSON字符串,进行反序列化的时候出错。用的是Newtonsoft.Json;
错误提示是“Cannot deserialize JSON object into type “System.Collections.Generic.List`1[”

string tem = kit.get("kdt.item.add", param);//tem保存返回的JSON
   addgoods = (AddGoodsClass)JsonConvert.DeserializeObject(c, (typeof(AddGoodsClass)));//反序列化

接收到的JSON

{"response":{"item":{"cid":8000017,"promotion_cid":0,"tag_ids":"","detail_url":"https:\/\/shop15609412.koudaitong.com\/v2\/showcase\/goods?alias=2g1e8skm0ztu4&from=wsc&kdtfrom=wsc","share_url":"https:\/\/shop15609412.koudaitong.com\/v2\/showcase\/goods?alias=2g1e8skm0ztu4&from=wsc&kdtfrom=wsc","skus":[],"pic_url":"","pic_thumb_url":"","num":"0","sold_num":0,"price":"200.07","post_type":1,"post_fee":"10.00","delivery_template_fee":"0.00","delivery_template_id":0,"delivery_template_name":"","item_imgs":[],"item_tags":[],"item_type":0,"is_supplier_item":false,"is_virtual":false,"is_listing":true,"is_lock":false,"is_used":false,"product_type":"0","auto_listing_time":"0","has_component":false,"template_id":0,"template_title":"\u666e\u901a\u7248","join_level_discount":"0","messages":[],"order":0,"purchase_right":0,"ump_tags":[],"ump_level":[],"ump_tags_text":null,"ump_level_text":null,"num_iid":"284756776","alias":"2g1e8skm0ztu4","title":"\u6d4b\u8bd5\u65b0\u589e\u5546\u54c11","desc":"","origin_price":"","outer_id":"","outer_buy_url":"","buy_quota":"0","created":"2016-07-25 09:59:31"}}}

接收JSON的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace youzanTest
{
    /// <summary>
    /// 新增商品JSON类
    /// </summary>
    class AddGoodsClass
    {
        public AddGoodsResponse response { get; set; }
    }
    public class AddGoodsResponse
    {
        //public AddGoodsItem item { get; set; }
        public List<AddGoodsItemPro> item { get; set; }
    }
    public class AddGoodsItem
    {
        public string cid { get; set; }
        public string promotion_cid { get; set; }
        public string tag_ids { get; set; }
        public string detail_url { get; set; }
        public string share_url { get; set; }
        public object[] skus { get; set; }
        public string pic_url { get; set; }
        public string pic_thumb_url { get; set; }
        public string num { get; set; }
        public string sold_num { get; set; }
        public string price { get; set; }
        public string post_type { get; set; }
        public string post_fee { get; set; }
        public string delivery_template_fee { get; set; }
        public string delivery_template_id { get; set; }
        public string delivery_template_name { get; set; }
        public object[] item_imgs { get; set; }
        public object[] item_tags { get; set; }
        public string item_type { get; set; }
        public string is_supplier_item { get; set; }
        public string is_virtual { get; set; }
        public string is_listing { get; set; }
        public string is_lock { get; set; }
        public string is_used { get; set; }
        public string product_type { get; set; }
        public string auto_listing_time { get; set; }
        public string has_component { get; set; }
        public string template_id { get; set; }
        public string template_title { get; set; }
        public string join_level_discount { get; set; }
        public object[] messages { get; set; }
        public string order { get; set; }
        public string purchase_right { get; set; }
        public object[] ump_tags { get; set; }
        public object[] ump_level { get; set; }
        public object ump_tags_text { get; set; }
        public object ump_level_text { get; set; }
        public string num_iid { get; set; }
        public string alias { get; set; }
        public string title { get; set; }
        public string desc { get; set; }
        public string origin_price { get; set; }
        public string outer_id { get; set; }
        public string outer_buy_url { get; set; }
        public string buy_quota { get; set; }
        public string created { get; set; }
    }
    public class AddGoodsItemPro
    {
        public string cid { get; set; }
        public string promotion_cid { get; set; }
        public string tag_ids { get; set; }
        public string detail_url { get; set; }
        public string share_url { get; set; }
        public object[] skus { get; set; }
        public string pic_url { get; set; }
        public string pic_thumb_url { get; set; }
        public string num { get; set; }
        public string sold_num { get; set; }
        public string price { get; set; }
        public string post_type { get; set; }
        public string post_fee { get; set; }
        public string delivery_template_fee { get; set; }
        public object[] item_imgs { get; set; }
        public object[] item_tags { get; set; }
        public string item_type { get; set; }
        public string is_supplier_item { get; set; }
        public string is_virtual { get; set; }
        public string is_listing { get; set; }
        public string is_lock { get; set; }
        public string is_used { get; set; }
        public string product_type { get; set; }
        public string auto_listing_time { get; set; }
        public string has_component { get; set; }
        public string template_id { get; set; }
        public string template_title { get; set; }
        public string join_level_discount { get; set; }
        public string num_iid { get; set; }
        public string alias { get; set; }
        public string title { get; set; }
        public string desc { get; set; }
        public string origin_price { get; set; }
        public string outer_id { get; set; }
        public string outer_buy_url { get; set; }
        public string buy_quota { get; set; }
        public string created { get; set; }
    }
}
解决方案

30

本人用vs2013自带功能,将Json粘贴为类,生成以下代码

public class Rootobject
    {
        public Response response { get; set; }
    }
    public class Response
    {
        public Item item { get; set; }
    }
    public class Item
    {
        public int cid { get; set; }
        public int promotion_cid { get; set; }
        public string tag_ids { get; set; }
        public string detail_url { get; set; }
        public string share_url { get; set; }
        public object[] skus { get; set; }
        public string pic_url { get; set; }
        public string pic_thumb_url { get; set; }
        public string num { get; set; }
        public int sold_num { get; set; }
        public string price { get; set; }
        public int post_type { get; set; }
        public string post_fee { get; set; }
        public string delivery_template_fee { get; set; }
        public int delivery_template_id { get; set; }
        public string delivery_template_name { get; set; }
        public object[] item_imgs { get; set; }
        public object[] item_tags { get; set; }
        public int item_type { get; set; }
        public bool is_supplier_item { get; set; }
        public bool is_virtual { get; set; }
        public bool is_listing { get; set; }
        public bool is_lock { get; set; }
        public bool is_used { get; set; }
        public string product_type { get; set; }
        public string auto_listing_time { get; set; }
        public bool has_component { get; set; }
        public int template_id { get; set; }
        public string template_title { get; set; }
        public string join_level_discount { get; set; }
        public object[] messages { get; set; }
        public int order { get; set; }
        public int purchase_right { get; set; }
        public object[] ump_tags { get; set; }
        public object[] ump_level { get; set; }
        public object ump_tags_text { get; set; }
        public object ump_level_text { get; set; }
        public string num_iid { get; set; }
        public string alias { get; set; }
        public string title { get; set; }
        public string desc { get; set; }
        public string origin_price { get; set; }
        public string outer_id { get; set; }
        public string outer_buy_url { get; set; }
        public string buy_quota { get; set; }
        public string created { get; set; }
    }

发现有一些字段类型不一致,你看看是不是专业原因


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明JSON 问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)