C# Json解析
来源:互联网 发布:java与php的区别 编辑:程序博客网 时间:2024/05/17 15:58
Json是一种轻量级数据交换格式,是一种理想的数据交换语言,易于理解和编写。
Json可以分为两部分:
1、Json Object(A Collection of name/value pairs)
2、Json Array(An ordered list of values)
使用LitJson解析Json。
using System.Text;using System.Threading.Tasks;using LitJson;namespace LitJsonTest{ class Program { static void Main(string[] args) { string str = "{'name':'cyf', 'id':10, 'items':[{'itemid':1001, 'itemname':'hello'},{'itemid':1002, 'itemname':'hello2'}]}"; //************读取Json字符串中的数据*********** JsonData jd = JsonMapper.ToObject(str); string name = (string) jd["name"]; Console.WriteLine("name: " + name); long id = Convert.ToInt64(jd["id"].ToString()); Console.WriteLine("id: " + id); JsonData jdItems = jd["items"]; Console.WriteLine("items: "); int itemCnt = jdItems.Count; foreach (JsonData item in jdItems) { int itemID = (int) item["itemid"]; string itemName = (string) item["itemname"]; Console.WriteLine("\titemid: " + itemID); Console.WriteLine("\titemname: " + itemName); Console.WriteLine(); } //string str2 = jd.ToJson(); Console.Read(); } }}
创建Json
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using LitJson;namespace LitJsonTest{ class Program { static void Main(string[] args) { JsonData data = new JsonData(); data["URL"] = "http;//localhost/index.php"; data["AssetList"] = new JsonData(); data["AssetList"]["id"] = 1; data["AssetList"]["name"] = "rechard"; data["items"] = new JsonData(); JsonData book01 = new JsonData(); book01["id"] = 1; book01["name"] = "Math"; JsonData book02 = new JsonData(); book02["id"] = 2; book02["name"] = "Englist"; data["items"].Add(book01); data["items"].Add(book02); string json_str = data.ToJson(); Console.WriteLine(json_str); Console.Read(); } }}输出结果:
博客链接:资源链接01 资源2
0 0
- C# 解析JSON -- Newtonsoft.Json
- C# 解析json Newtonsoft.json
- c#解析JSON总结
- C#解析JSON
- C# 解析 json
- C#解析Json
- c# 解析json数据
- C# 解析 json
- C# 实现json解析
- C#解析JSON
- c# 解析json(整理)
- c# json解析
- C# Json解析
- C#解析JSON
- C# json 解析
- C# 解析 Json数据
- C# 解析 json
- C#解析json
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- readonly 和 const 的区别和使用
- Intro.js使用方法
- java工厂模式简单实例
- 展开的listVIew
- C# Json解析
- LEGACY BIOS和EFI区别
- Map线程安全几种实现方法
- ps aux指令詳解
- Netty的ChannelFuture
- Oracle中INSTR和SUBSTR的用法
- dddddddddddddddd
- 广告精准投放笔记
- matlab求导数