首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么将阿里巴巴接口返回来的JSON数据转化为C#的对像? 急求

2013-03-13 
如何将阿里巴巴接口返回来的JSON数据转化为C#的对像? 急求var json {result:{total: 38,toReturn:

如何将阿里巴巴接口返回来的JSON数据转化为C#的对像? 急求


    var json = {
        "result":
            {
                "total": 38,
                "toReturn":
                  [{
                      "sellerAlipayId": "2088302179419572",
                      "tradeType": "6",
                      "orderEntries":
                          [{
                              "id": 306623282075090,
                              "price": 2100,
                              "entryStatus": "waitsellersend",
                              "productPic": [
                                  "http://img.china.alibaba.com:80//img/order/trading/090/570/282326603/657902513_2115513935.310x310.jpg"
                              ],
                              "quantity": 1.0,
                              "productName": "日韩打底衫热销休闲时尚修身秋冬女装批发2011秋季新款长袖t恤",
                              "specInfo": [
                                  { "specValue": "灰色", "specName": "颜色" },
                                  { "specValue": "XL", "specName": "尺码" }
                              ],
                              "entryDiscount": 0,


                              "sourceId": 1194532438
                          },
                          {
                              "id": 306623282085090, "price": 2100, "entryStatus": "waitsellersend", "productPic": ["http://img.china.alibaba.com:80//img/order/trading/090/580/282326603/657902513_2115513935.310x310.jpg"], "quantity": 1.0, "productName": "日韩打底衫热销休闲时尚修身秋冬女装批发2011秋季新款长袖t恤", "specInfo": [{ "specValue": "灰色", "specName": "颜色" }, { "specValue": "L", "specName": "尺码" }], "entryDiscount": 0, "sourceId": 1194532438
                          },
                          {
                              "id": 306623282095090, "price": 2500, "entryStatus": "waitsellersend", "productPic": ["http://img.china.alibaba.com:80//img/order/trading/090/590/282326603/639300751_2115513935.310x310.jpg"], "quantity": 1.0, "productName": "日韩打底衫热销休闲时尚修身秋冬女装批发2011秋季新款长袖t恤", "specInfo": [{ "specValue": "米黄", "specName": "颜色" }, { "specValue": "XL", "specName": "尺码" }], "entryDiscount": 0, "sourceId": 1189375286
                          },
                          {
                              "id": 306623282105090, "price": 2500, "entryStatus": "waitsellersend", "productPic": ["http://img.china.alibaba.com:80//img/order/trading/090/501/282326603/639300751_2115513935.310x310.jpg"], "quantity": 1.0, "productName": "日韩打底衫热销休闲时尚修身秋冬女装批发2011秋季新款长袖t恤", "specInfo": [{ "specValue": "米黄", "specName": "颜色" }, { "specValue": "L", "specName": "尺码" }], "entryDiscount": 0, "sourceId": 1189375286
                          }],
                      "gmtCreate": "2013-03-09 22:33:41",
                      "alipayTradeId": "2013030903674879",
                      "carriage": 1000,
                      "status": "waitsellersend",


                      "toPost": "050000",
                      "buyerAlipayId": "2088702315601799",
                      "sumPayment": 10200,
                      "buyerMemberId": "b2b-780439050",
                      "toArea": "河北省 石家庄市 桥西区 西里街XXXXX",
                      "discount": 0,
                      "id": 306623282065090,
                      "toMobile": "15130XXXXXX",
                      "sellerMemberId": "xiaogoXXXXXX",
                      "gmtPayment": "2013-03-09 22:35:01",
                      "toFullName": "XXX"
                  }],
                "success": true
            }
    };


如阿里巴巴返回了如上的一段字符串JSON,上面是我对这个字符串进行格式化的结果 
那我用什么方法可以最简单的转化为C#可访问形式的对像呢?如
result.toReturn[0].sellerAlipayId 得到 2088302179419572

万分感谢
[解决办法]
可以用Json.net反序列化为JObject对象,通过索引访问。如果数据模型比较固定也可以自己先建立一个类去反序列化。
[解决办法]
你需要根据对这个json的理解,自定义一个class,例如
public class myDataType
{
    public myResultType result;
}

public class myResultType
{
    public int total;
    public bool success;
    public myReturnType[] toReturn;
}


public class myReturnType
{
    public string sellerAlipayId;
    ......... 
}


其中 ..... 部分与其它部分一样,都是根据你的理解来定义属性和子类型。如果不定义,那么也无妨,只不过是解析时忽略掉那个属性而已。

一旦定义了class,那么使用一句代码就可以从字符串反序列出.net内部对象了。你可以使用.net内置的json反序化方法,或者任何第三方json处理的类库来反序列化。
[解决办法]
vs2012 里面有个微软维护的叫ASP.NET and Web Tools 2012.2的插件,其中一个功能就是在web项目中提供了2个扩展功能

选择性粘贴--》将xml自动粘贴成class
          ---》将json字符串自动粘贴成class

下面就是我把你json字符串,直接粘贴到vs后,由这个插件自动生成的class

   public class Rootobject
    {
        public Result result { get; set; }


    }

    public class Result
    {
        public int total { get; set; }
        public Toreturn[] toReturn { get; set; }
        public bool success { get; set; }
    }

    public class Toreturn
    {
        public string sellerAlipayId { get; set; }
        public string tradeType { get; set; }
        public Orderentry[] orderEntries { get; set; }
        public string gmtCreate { get; set; }
        public string alipayTradeId { get; set; }
        public int carriage { get; set; }
        public string status { get; set; }
        public string toPost { get; set; }
        public string buyerAlipayId { get; set; }
        public int sumPayment { get; set; }
        public string buyerMemberId { get; set; }
        public string toArea { get; set; }
        public int discount { get; set; }
        public long id { get; set; }
        public string toMobile { get; set; }
        public string sellerMemberId { get; set; }
        public string gmtPayment { get; set; }
        public string toFullName { get; set; }
    }

    public class Orderentry
    {
        public long id { get; set; }
        public int price { get; set; }
        public string entryStatus { get; set; }
        public string[] productPic { get; set; }
        public float quantity { get; set; }
        public string productName { get; set; }
        public Specinfo[] specInfo { get; set; }
        public int entryDiscount { get; set; }
        public int sourceId { get; set; }
    }

    public class Specinfo


    {
        public string specValue { get; set; }
        public string specName { get; set; }
    }



ps:这个插件已经出来有段时间了,只是以前是bete版,选择性粘贴不支撑非e文区字符集设定,你用的时候需要去在“区域和语言”里换到e文区,这个比较麻烦,所以我们没有推荐使用,不过最近这个插件刚刚升级成正式版,所以已经可以正常使用了,我可以推荐大家装一下

这东西一个简单介绍:http://www.infoq.com/cn/news/2013/02/asp-net-web-tools-2012-2
官方下载:http://www.asp.net/vnext

热点排行