- 创建项目:ConsoleApplication_Json
- 选择项目右键管理NuGet程序包
- 搜索Newtonsoft.Json 并下载安装
- 选择项目添加 JsonHelper.cs
- 添加引用:
using Newtonsoft.Json;
- .编写Json处理方法代码,如下:
using System;
using Newtonsoft.Json;
namespace ConsoleApplication_Json
{
public class JsonHelper
{
public static decimal ToDecimal(object obj, decimal defaultValue = 0)
{
string s = obj == null ? "" : obj.ToString();
decimal result;
if (!decimal.TryParse(s, out result))
{
result = defaultValue;
}
return result;
}
public static string Obj2Json(object value)
{
return JsonConvert.SerializeObject(value);
}
///
/// 将JSON 字符串转换成对应的类型
///
/// 转换后的类型
/// 需转换的字符串
///
public static T Json2Obj(string s)
{
try
{
return JsonConvert.DeserializeObject(s);
}
catch (Exception ex)
{
return default(T);
}
}
}
}
- .自定义Json格式模拟数据
{
"id": "ko09895dd6g565d5222242125",
"status": true,
"httpstatus": 200,
"data": [{
"room": {
"field1": "955gg2015",
"field2": "G1020",
"field3": "VIP",
"field4": "大包",
"field5": "李春凤",
"field6": "10086611"
},
"address": "这里是个地址信息",
"sign": "预订"
}, {
"room": {
"field1": "955gg2016",
"field2": "G1022",
"field3": "VIP_null",
"field4": "中等包",
"field5": "李春凤",
"field6": "10086622"
},
"address": "这里是个地址信洗第二个",
"sign": "预订"
}],
"nextLink": "www.baidu.com"
}
- .格式化Json字符串用于测试
{\"id\": \"ko09895dd6g565d5222242125\", \"status\": true, \"httpstatus\": 200, \"data\": [{ \"room\": { \"field1\": \"955gg2015\", \"field2\": \"G1020\", \"field3\": \"VIP\", \"field4\": \"大包\", \"field5\": \"李春凤\", \"field6\": \"10086611\" }, \"address\": \"这里是个地址信息\", \"sign\": \"预订\" }, { \"room\": { \"field1\": \"955gg2016\", \"field2\": \"G1022\", \"field3\": \"VIP_null\", \"field4\": \"中等包\", \"field5\": \"李春凤\", \"field6\": \"10086622\" }, \"address\": \"这里是个地址信洗第二个\", \"sign\": \"预订\" }], \"nextLink\": \"www.baidu.com\" }
- .新建刚才与Json格式对应的类:RoomResponse.cs,代码如下
using System.Collections.Generic;
namespace ConsoleApplication_Json
{
public class RoomResponse
{
public string id { get; set; }
public string status { get; set; }
public string httpstatus { get; set; }
public List data { get; set; }
public string nextLink { get; set; }
}
public class Data
{
public Room room { get; set; }
public string address { get; set; }
public string sign { get; set; }
}
public class Room
{
public string field1 { get; set; }
public string field2 { get; set; }
public string field3 { get; set; }
public string field4 { get; set; }
public string field5 { get; set; }
public string field6 { get; set; }
}
}
- .进入Program.cs 文件,编辑代码如下:
namespace ConsoleApplication_Json
{
class Program
{
static void Main(string[] args)
{
var strJson = "{\"id\":\"ko09895dd6g565d5222242125\",\"status\":true,\"httpstatus\":200,\"data\":[{\"room\":{\"field1\":\"955gg2015\",\"field2\":\"G1020\",\"field3\":\"VIP\",\"field4\":\"大包\",\"field5\":\"李春凤\",\"field6\":\"10086611\"},\"address\":\"这里是个地址信息\",\"sign\":\"预订\"},{\"room\":{\"field1\":\"955gg2016\",\"field2\":\"G1022\",\"field3\":\"VIP_null\",\"field4\":\"中等包\",\"field5\":\"李春凤\",\"field6\":\"10086622\"},\"address\":\"这里是个地址信洗第二个\",\"sign\":\"预订\"}],\"nextLink\":\"www.baidu.com\"}";
var test1 = JsonHelper.Json2Obj(strJson);
}
}
}
- .加断点,调试文件。
- .数据成功转换成类型对象。
- 补充:
///
/// 使用JObjectParse
/// 获取Json字符串中的某个值
///
///
///
public static JObject JObjectParse(string s)
{
try
{
var obj = JObject.Parse(s);
return obj;
}
catch (Exception)
{
return null;
}
}
///
/// 使用JavaScriptSerializer
/// 获取Json字符串中的某个值
///
///
///
public static Dictionary JavaScriptSerializer(string s)
{
try
{
JavaScriptSerializer Jss = new JavaScriptSerializer();
Dictionary DicText = (Dictionary)Jss.DeserializeObject(s);
return DicText;
}
catch (Exception ex)
{
throw;
}
}
- 该方法用于获取每个节点对应的内容,可直接通过obj[“data”]取出数据。
- 先这样吧。