C#JSON解析 [英] C# JSON Parsing
本文介绍了C#JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有麻烦牧师一些JSON数据:
{
头衔:LED侧转向信号灯9-33V 6类电缆19厘米铬基LED汽车灯具
user_price:{
inc_tax_value:340.67,
ex_tax_value:30.97,
base_inc_tax:340.67,
base_ex_tax:30.97,
currency_ code:,
price_rule:35
},
local_quantity:0,
global_quantity:0,
URL:http://ishop2.cooldrive.com.au/products/3102CM
all_warehouse_quantity:0,
说明1:LED侧方向标,
内容描述:9-33V 6类,19厘米电缆,
description3:铬基LED汽车灯具
price_per:1,
default_pack_quantity:1,
code:3102CM
}
我已经尝试了一些例子,但我不能从产品信息这个例子中得到任何数据。
这是我想使用:
字符串testJson =新HttpHelper()POST(http://ishop2.cooldrive.com.au/api/product,code = 3102CM)。
System.Windows.Forms.Clipboard.SetText(testJson);
MyJsonClass测试=新System.Web.Script.Serialization.JavaScriptSerializer()
.Deserialize&所述; MyJsonClass>(@ testJson);
正文返回的权利如上但testJson总是空...
私有类MyJsonClass
{
公众的IList<&IDictionary的LT;字符串,字符串>>数据{搞定;组; }
}
解决方案
这与您的JSON
VAR字典=新的JavaScriptSerializer()反序列化<&字典LT;字符串对象>>(testJson);Console.WriteLine(快译通[URL]);VAR价格=(词典<字符串对象>)字典[user_price];
Console.WriteLine(价格[inc_tax_value]);
I am having trouble parson some JSON data:
{
"title": "LED SIDE DIRECTION INDICATOR 9-33V CATEGORY 6, 19cm CABLE CHROME BASE LED AUTO LAMPS",
"user_price": {
"inc_tax_value": 34.067,
"ex_tax_value": 30.97,
"base_inc_tax": 34.067,
"base_ex_tax": 30.97,
"currency_code": "",
"price_rule": "35"
},
"local_quantity": 0,
"global_quantity": 0,
"url": "http://ishop2.cooldrive.com.au/products/3102CM",
"all_warehouse_quantity": 0,
"description1": "LED SIDE DIRECTION INDICATOR",
"description2": "9-33V CATEGORY 6, 19cm CABLE",
"description3": "CHROME BASE LED AUTO LAMPS",
"price_per": 1,
"default_pack_quantity": 1,
"code": "3102CM"
}
I have tried a number of example but I cannot get any data from this example of product information. here is what I am trying to use:
string testJson = new HttpHelper().POST("http://ishop2.cooldrive.com.au/api/product", "code=3102CM");
System.Windows.Forms.Clipboard.SetText(testJson);
MyJsonClass tester = new System.Web.Script.Serialization.JavaScriptSerializer()
.Deserialize<MyJsonClass>(@testJson);
The text return right as above but the testJson is always null...
private class MyJsonClass
{
public IList<IDictionary<string, string>> data { get; set; }
}
解决方案
This works with your json
var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(testJson);
Console.WriteLine(dict["url"]);
var price = (Dictionary<string, object>)dict["user_price"];
Console.WriteLine(price["inc_tax_value"]);
这篇关于C#JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文