c如何解析json数据

c如何解析json数据,第1张

你知道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)

首先要知道json 的格式 把dataset中datatable表中数据转成json格式 public static string JsonConvert(DataSet ds)

{

StringBuilder sb = new StringBuilder()

sb.Append("[")

for (int i=0i<ds.Tables[0].Rows.Counti++)

{

sb.Append("{")

for (int j = 0j <ds.Tables[0].Columns.Countj++)

{

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

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10707617.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-10
下一篇 2023-05-10

发表评论

登录后才能评论

评论列表(0条)

保存