您的位置:首页 > Web前端 > JavaScript

C# 新手项目学习5 --Newtonsoft.Json Json解析

2014-04-18 18:31 369 查看
JSON解析

用Newtonsoft.Json.dll 下载  http://json.codeplex.com/

1:简单的单层JSON解析

 如: //"{  \"result\": 1,  \"adminId\": 2,  \"customerId\": 1}"

        

JObject jobj = JObject.Parse(resultStr);
int result = (int)jobj["result"];
int adminId = (int)jobj["adminId"];
int customerId = (int)jobj["customerId"];
2:简单的双层解析

如:


JObject jo = JObject.Parse(result);
int resulta = (int)jo["result"];
JArray jas = (JArray)jo["customers"];
string createTime = jas[0]["createTime"].ToString();
int customerId = (int)jas[0]["customerId"];
int parkingCount = (int)jas[0]["parkingCount"];
int type = (int)jas[0]["type"];
string companyName = jas[0]["companyName"].ToString();
string modifyTime = jas[0]["modifyTime"].ToString();


如果存在内容有



类似这种的,就只要知道rechargeRecord下面有多少个数组就好了。Jraay有一个Count的方法,可以得到个数

JArray jas = (JArray)jo["rechargeRecord"];
int joa = jas.Count();
for (int i = 0; i < joa; i++)
{
string createTime = (string)jas[i]["createTime"];
DateTime createDate = GetNoralTime(createTime);
int insideCarId = (int)jas[i]["insideCarId"];
int customerId = (int)jas[i]["customerId"];
int vChargeRecrodId = (int)jas[i]["vChargeRecrodId"];
int type = (int)jas[i]["type"];
int rechargeRecordId = (int)jas[i]["rechargeRecordId"];
string sql = "";
if (type == 0)
sql = "insert into tb_rechargeRecrod values (" + rechargeRecordId + "," + customerId + "," + insideCarId + "," + vChargeRecrodId + ",'" + createDate + "')";
else
sql = "update tb_rechargeRecrod set createTime='" + createTime + "' where rechargeRecordId=" + rechargeRecordId;
DataBase.UpdateDB(sql);
}

这样就可以得到每个数组的数据。。。嘎嘎
嗯。自己瞎搞出来的。感觉怪怪的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息