JavaScriptSerializer DeserializeObject 这两个是.net内置函数,这里不做介绍。
下面帖一段Newtonsoft.Json 6版本配合.net4.0框架的使用范例。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
//JSON数据解析
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
public partial class json_test001 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//writerjson();
//readjson();三层结构
readjson2();
}
/**单层JSON数据结构测试*/
protected void readjson2()
{
/**
* JSON数据说明
* []开头结尾
* {}里的数据可以直接读
* 一个[]可以转换为数组{a:a}{b:b}{c:c}
* 一个圆括号是对象,{}里面是数组
* */
StringBuilder sb = new StringBuilder();
string str = "[{ID:'1',Name:'John',Other:[{Age:'21',Sex:[{name:'prospertutt1'},{name:'prospertutt2'},{name:'prospertutt3'}]}]},{ID:'2',Name:'Good',Other:[{Age:'36',Sex:'1'}]}]";
JArray javascript = (JArray)JsonConvert.DeserializeObject(str);
//修改json数据
javascript[0]["Name"] = "Prospertu涂";
//Response.Write(javascript);
//第二层
JObject Bobj0 = (JObject)javascript[0];//{ID:'1',Name:'John',Other:[{Age:'21',Sex:'0'}]}
JArray Bjson0 = (JArray)Bobj0["Other"];//[{Age:'21',Sex:'0'}]
JObject Bobj1 = (JObject)Bjson0[0];//{Age:'21',Sex:'0'} **注意层数从0开始算起,2层=1
Bobj1["Age"] = "28"; //修改数据
// Response.Write(javascript); //=str修改后的数据
// Response.Write(Bjson0[0]); // = Bobj1修改后的数据 = { "Age": "28", "Sex": "0" }
// Response.Write(Bobj1["Age"]); //=28 区分大小写 参数不能用0
//第三层
JObject Cobj0 = (JObject)javascript[0];//{ID:'1',Name:'John',Other:[{Age:'21',Sex:[{....}]}]}
JArray Cjson0 = (JArray)Cobj0["Other"];//[{Age:'21',Sex:[{...}]}]
JObject Cobj1 = (JObject)Cjson0[0];//{Age:'21',Sex:[{}]} **注意层数从0开始算起,2层=1
JArray Cjson1 = (JArray)Cobj1["Sex"];//[{name:'prospertu'}]
// JObject Cobj2 = (JObject)Cjson1[0];//{"name":"873027832"}
// Cobj2["name"] = "873027832";
// Response.Write(Cjson1[0]);
// Response.Write(Cobj2["name"]);
//Cobj2.Remove();//删除Sex下的某个{}此为按索引的情况
//按字段名的情况如下:
for (int Ci = 0; Ci < Cjson1.Count; Ci++)
{
JObject Cobj2 = (JObject)Cjson1[Ci];
if (Cobj2["name"].ToString() == "prospertutt3" && true)
{
Cobj2.Remove();//删除Sex下的某个{}
}
}
Response.Write(Cobj1);
for (int i0 = 0; i0 < javascript.Count; i0++) //循环获得小括号对象
{
// sb.Append("ID:" + obj["people"].ToString());
// sb.Append(" Name:" + obj["Name"].ToString());
JObject obj0 = (JObject)javascript[i0]; //获得小的对象{}
if (i0 == 3)
{
JArray json0 = (JArray)obj0["regions"]; //获得第2层数组[第4个]
// for (int i1 = 0; i1 < json0.Count; i1++)
// {
JObject obj1 = (JObject)json0[0];//i1 获得小小的对象
JArray json1 = (JArray)obj1["apps"]; //获得第3层数组
for (int i2 = 0; i2 < json1.Count; i2++)
{
JObject obj2 = (JObject)json1[i2]; //得到对象{}为一对象
sb.Append("<BR /> name:" + obj2["name"].ToString());
sb.Append("|| appkey:" + obj2["appKey"].ToString());
}
// }
}
}
Response.Write(sb.ToString());
}
//读取json 三层结构
protected void readjson()
{
// JsonObject newObj = new JsonObject(s_lmWeiZhi);
// Response.Write(Console.WriteLine(newObj["orders"]["books"][1]["name"].Value));
//json解析
StringBuilder sb = new StringBuilder();
string str = "[{ID:'1',Name:'John',Other:[{Age:'21',Sex:'0'}]},{ID:'2',Name:'Good',Other:[{Age:'36',Sex:'1'}]}]";
JArray javascript = (JArray)JsonConvert.DeserializeObject(str);
//javascript["id"].Value = 2;
// javascript.Value<name>="abc";
// Response.Write(str);
for (int i0 = 0; i0 < javascript.Count; i0++ ) //循环获得小括号对象
{
// sb.Append("ID:" + obj["people"].ToString());
// sb.Append(" Name:" + obj["Name"].ToString());
JObject obj0 = (JObject)javascript[i0]; //获得小的对象{}
if (i0 == 3)
{
JArray json0 = (JArray)obj0["regions"]; //获得第2层数组[第4个]
// for (int i1 = 0; i1 < json0.Count; i1++)
// {
JObject obj1 = (JObject)json0[0];//i1 获得小小的对象
JArray json1 = (JArray)obj1["apps"]; //获得第3层数组
for (int i2 = 0; i2 < json1.Count; i2++)
{
JObject obj2 = (JObject)json1[i2]; //得到对象{}为一对象
sb.Append("<BR /> name:" + obj2["name"].ToString());
sb.Append("|| appkey:" + obj2["appKey"].ToString());
}
// }
}
}
Response.Write(sb.ToString());
}
protected void writerjson()
{
/*
StringWriter sw = new StringWriter();
JsonWriter writer = new JsonWriter(sw);
writer.WriteStartArray();
writer.WriteValue("JSON!");
writer.WriteValue(1);
writer.WriteValue(true);
writer.WriteStartObject();
writer.WritePropertyName("property");
writer.WriteValue("value");
writer.WriteEndObject();
writer.WriteEndArray();
writer.Flush();
string jsonText = sw.GetStringBuilder().ToString();
Console.WriteLine(jsonText);
*/
}
}
===========================================
读取多层JSON结构[{"aa":{"aa1":"bb1"}}]
string aa = json[0]["aa"]["aa1"]
============================================
JArray json_menu_update = (JArray)JsonConvert.DeserializeObject(myUser_set_menu);JObject obj111 = (JObject)json_menu_update[0];
JArray obj222 = (JArray)obj111["online"];
JObject obj333 = (JObject)obj222[7];
json_menu_update.Add((JObject)JsonConvert.DeserializeObject("{'a':'b'}"));//在顶层新增一个JSON
obj222.Add((JObject)JsonConvert.DeserializeObject("{'a':'b'}"));//在2层新增一个JSOn
obj2["appKey"] = appkey;//修改
obj2.Remove(); //不能用RemoveAll()会留下一个{},这样的符号
JValue javascripts = (JValue)JsonConvert.SerializeObject(javascript); //去除空格
//修改json start (类似 修改"content":[{"a":1}]为content":[{"b":33}]这样)
JArray javascript = (JArray)JsonConvert.DeserializeObject(conSetInfo);
javascript[0]["content"] = (JArray)JsonConvert.DeserializeObject("[" + jsonALL_V + "]");
conSetInfo = Convert.ToString((JValue)JsonConvert.SerializeObject(javascript)); //去除空格
//修改json end
============================================
下面帖一个转换函数,包含去除空格的代码
public static string json(string jsonstr,string id, string layoutId, string type,string name,string appkey, string dz)
{
//json解析
//StringBuilder sb = new StringBuilder();
//string jsonstr = "[{ID:'1',Name:'John',Other:[{Age:'21',Sex:'0'}]},{ID:'2',Name:'Good',Other:[{Age:'36',Sex:'1'}]}]";
JArray javascript = (JArray)JsonConvert.DeserializeObject(jsonstr);
//javascript["id"].Value = 2;
// javascript.Value<name>="abc";
// Response.Write(str);
for (int i0 = 0; i0 < javascript.Count; i0++) //循环获得小括号对象
{
// sb.Append("ID:" + obj["people"].ToString());
// sb.Append(" Name:" + obj["Name"].ToString());
if ((string)javascript[i0]["layoutId"] == layoutId && (string)javascript[i0]["id"]==id )
{
JObject obj0 = (JObject)javascript[i0]; //获得小的对象{}
JArray json0 = (JArray)obj0["regions"]; //获得第2层数组[第4个]
for (int i1 = 0; i1 < json0.Count; i1++)
{
//System.Web.HttpContext.Current.Response.Write(json0[i1]["type"]);
//System.Web.HttpContext.Current.Response.End();
if ((string)json0[i1]["type"] == type)
{
JObject obj1 = (JObject)json0[i1];//i1 获得小小的对象
JArray json1 = (JArray)obj1["apps"]; //获得第3层数组
for (int i2 = 0; i2 < json1.Count; i2++)
{
JObject obj2 = (JObject)json1[i2]; //得到对象{}为一对象
//sb.Append("<BR /> name:" + obj2["name"].ToString());
//sb.Append("|| appkey:" + obj2["appKey"].ToString());
switch (dz)
{
case "update":
//更新json
if ((string)obj2["name"] == name)
{
obj2["appkey"] = appkey;
}
break;
case "delete":
//删除json
if ((string)obj2["name"] == name && (string)obj2["appkey"]==appkey)
{
obj2.Remove();
}
break;
default:
break;
}
}
}
}
}
}
//sb.Append(javascript);
//System.Web.HttpContext.Current.Response.Write(javascript);
//JArray javascript = (JArray)JsonConvert.DeserializeObject(jsonstr);
JValue javascripts = (JValue)JsonConvert.SerializeObject(javascript); //去除空格
return javascripts.ToString();
}
}