C#中怎么解析JSON数据?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中怎么解析JSON数据?相关的知识,希望对你有一定的参考价值。
参考技术A 首先要知道json 的格式 把dataset中datatable表中数据转成json格式 public static string JsonConvert(DataSet ds)StringBuilder sb = new StringBuilder();
sb.Append("[");
for (int i=0;i<ds.Tables[0].Rows.Count;i++)
sb.Append("");
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
sb.Append("");
sb.Append(ds.Tables[0].Columns[j].ColumnName);
sb.Append(":\"");
sb.Append(ds.Tables[0].Rows[i][j].ToString().Trim());
sb.Append("\",");
sb.Remove(sb.Length-1,1);
sb.Append(",");
sb.Remove(sb.Length-1,1);
sb.Append("]");
return sb.ToString();
/// <summary>
/// 将Ilist转成json格式
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static string JsonConvert(IList<TransBody> list)
try
System.Runtime.Serialization.Json.DataContractJsonSerializer seializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(list.GetType());
using (MemoryStream ms = new MemoryStream())
seializer.WriteObject(ms, list);
return Encoding.UTF8.GetString(ms.ToArray());
catch
return null;
把json格式转成ILIST 格式
public static object ConvertJson(string json ,Type t)
try
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(t);
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
return serializer.ReadObject(ms);
catch
return null;
参考技术B .net 3.5 下有个system.web.script.serialization,有个类,javascriptSerializer 详情参考: http://hi.baidu.com/%B1%F9%D1%A9%D2%F8%C1%AB/blog/item/cfd4cf25cecddc228644f91c.html
c#中怎么解析多层json数据
参考技术A 在.net 2.0中提取这样的json"name":"lily","age":23,"addr":"city":guangzhou,"province":guangdong
引用命名空间
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
可以把上面的JSON看成一个对象.你只要写对应的类即可
public class UserInfo
public string name;
public int age;
public address addr;
public class address
public string city;
public string province;
然后在解析的地方这样写:
string jsonData="\"name\":\"lily\",\"age\":23,\"addr\":\"city\":guangzhou,\"province\":guangdong";
UserInfo user=(UserInfo)JsonConvert.DeserializeObject(jsonData, typeof(UserInfo));
得到City的值只要:user.addr.City;
这样实现也行
JObject jsonObj = JObject.Parse(jsonData);
string name=jsonObj ["name"].ToString();
string age=jsonObj ["age"].ToString();
string city=((JObject )jsonObj ["addr"])["city"].ToString();
string province=((JObject )jsonObj ["addr"])["province"].ToString();
如 何这个json是动态的呢?譬如让你输入一个json,如"name":"lily","age":23,"addr": "city":guangzhou,"province":guangdong; 然后让你输入一个对象,如city,然后系统会输出guangzhou这个值,那这样的话,json就是动态生成的了,我想了解有没有读取这样的json 的方法。(注意,json是多级嵌套的。)
就用遍历
public string GetJsonValue(JEnumerable<JToken> jToken,string key)
IEnumerator enumerator = jToken.GetEnumerator();
while (enumerator.MoveNext())
JToken jc = (JToken)enumerator.Current;
if (jc is JObject||((JProperty)jc).Value is JObject)
return GetJsonValue(jc.Children(), key);
else
if (((JProperty)jc).Name == key)
return ((JProperty)jc).Value.ToString();
return null;
在调用的时候:
string jsonData = "\"name\":\"lily\",\"age\":23,\"addr\":\"city\":\"guangzhou\",\"province\":\"guangdong\"";
JObject jsonObj = JObject.Parse(jsonData);
Response.Write(GetJsonValue(jsonObj.Children(), "province"));
如果有多层嵌套的数组
string jsonData = "\"addr\":[\"city\":\"guangzhou\",\"province\":\"guangdong\",\"city\":\"guiyang\",\"province\":\"guizhou\"]";
JObject jsonObj = JObject.Parse(jsonData);
JArray jar = JArray.Parse(jsonObj["addr"].ToString());
JObject j = JObject.Parse(jar[0].ToString());
Response.Write(j["city"]);
JSON转XML
string xmlstr=((XmlDocument)JsonConvert.DeserializeXmlNode(jsonData)).InnerXml.ToString();本回答被提问者采纳
以上是关于C#中怎么解析JSON数据?的主要内容,如果未能解决你的问题,请参考以下文章