本文目录一览:
c如何解析json数据
你知道json的逻辑结构吗?
JSONObjectjson=newJSONObject();
JSONArrayjsonMembers=newJSONArray();
for(Deptdaily:dailys){
JSONObjectmember=newJSONObject();
member.put("dept_name",daily.getDPT_NAME());
member.put("user_name",daily.getUser_name());
if(daily.getE_mail()==null){
daily.setE_mail("暂无");
}
member.put("email",daily.getE_mail());
member.put("daily_date",daily.getDaily_date());
jsonMembers.put(member);
}
JSONArrayjsonMembers1=newJSONArray();
JSONObjectmember1=newJSONObject();
member1.put("pagesize",dept.getPagesize());
member1.put("allpage",dept.getAllpage());
member1.put("allitems",dept.getAllitems());
member1.put("nowpage",dept.getNowpage());
jsonMembers1.put(member1);
json.put("check",jsonMembers);
json.put("page",jsonMembers1);
out.print(json);
C#中怎么解析JSON数据?
首先要知道json 的格式 把dataset中datatable表中数据转成json格式 public static string JsonConvert(DataSet ds)
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
for (int i=0;ids.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(IListTransBody 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;
}
}
c#解析JSON的几种办法
对比
准备数据
实体类:
定义:
使用DataContractJsonSerializer
帮助类:
用法:
输出:
使用JavaScriptSerializer
// using System.Web.Script.Serialization;
var jser = new JavaScriptSerializer();
var json = jser.Serialize(new ListPerson() { p1, p2 });
var persons = jser.DeserializeListPerson(json);
使用Silverlight
使用JSON.NET
输出:
LINQ:
其他:
输出: