.net调用json

数据如下:

{"cacheCount":1,"count":"34","slice":"5, 5","list":[1001598,1001601,1001605,1001609,1001612],"page":1,"error":200}

 

采用第三方组件

Jayrock 和 Jayrock.Json

首先引入命名空间

using Jayrock.Json;

其次,创建 JsonObject 对象,步骤如下:

string strJsonText = @"{"cacheCount":1,"count":"34","slice":"5, 5","list":

[1001598,1001601,1001605,1001609,1001612],"page":1,"error":200}";

JsonReader reader = new JsonTextReader(new StringReader(strJsonText));

JsonObject jsonObj = new JsonObject();
jsonObj.Import(reader);

这样,就将一个文本的JSon数据转变成一个对象,如果要获取 count 的值,则可以这样

string count = jsonObj["count"].ToString();

但是有个问题,list 是一个数组,该如何获取呢?不用急,Jayrock已经为我们准备好了,来看

using (JsonTextReader textReader = new JsonTextReader(new StringReader(jsonObj["list"].ToString())))
{
    while (textReader.Read())
    {
        if (!string.IsNullOrEmpty(textReader.Text))
        {
            Response.Write(textReader.Text);
        }
    }
}

将数组的内容再赋予一个JsonTextReader对象 ,利用其Read方法进行逐行读取就OK了

当然,你也可以使用 JsonArray 对象,这里就不再叙述了

protected void Button1_Click(object sender, EventArgs e)
{
    string str = "{\"order\":{\"orderNO\":\"PO08120200038\",\"postTime\":\"2008-12-2 15:08:36\",\"sender\":\"联想\",\"receiver\":\"华为科技有限公司a\",\"agent\":\"深圳市怡亚通供应链股份有限公司\",\"customerOrderNO\":\"sdfdsf\",\"senderLinkman\":\"吴可立联想\",\"receiverLinkman\":\"所有商务人员\",\"productList\":[{\"productName\":\"商品3\",\"productQuantity\":\"34\",\"productUnitPrice\":\"34\",\"productTotalPrice\":\"1156\"}],\"orderTotalPrice\":\"1,156.0000\",\"orderCurreny\":\"人民币\",\"balanceCurreny\":\"人民币\",\"fetchAddress\":\"\",\"deliveryMode\":\"\",\"deliveryTime\":\"\",\"receiverLinkman\":\"\",\"receiverContact\":\"\",\"orderDescript\":\"dfsdf\",\"orderRemark\":\"\"}}";
    //解密Json,获取数据Begin
    //string hxw = CACheck.DecodeCA(this.hidCAString.Value);
    JsonReader reader = new JsonTextReader(new StringReader(str));
    
    JsonObject jsonObj = new JsonObject();
    jsonObj.Import(reader);
    
    //这样,就将一个文本的JSon数据转变成一个对象,如果要获取 count 的值,则可以这样
    
    string count = jsonObj["order"].ToString();
    
    JsonReader reader2 = new JsonTextReader(new StringReader(count));
    JsonObject jsonObj2 = new JsonObject();
    jsonObj2.Import(reader2);
    
    string count2 = jsonObj2["orderNO"].ToString();
    string count3 = jsonObj2["postTime"].ToString();
    
    Response.Write(count2+"---"+count3);
}

 

转载于:https://www.cnblogs.com/CoderWayne/archive/2008/05/07/4485583.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值