NewTonJson.Net的使用

时间:2024-11-22 20:33:20

1.引用“Newtonsoft.Json” 4.0 版本

2.基础类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace NewTonJsonTest
{
public class Animal
{ public string nickname { get; set; }
public int age { get; set; }
}
}

3.JsonHelper 辅助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace NewTonJsonTest
{
/// <summary>
/// Json帮助类
/// </summary>
public class JsonHelper
{
/// <summary>
/// 将对象序列化为JSON格式
/// </summary>
/// <param name="o">对象</param>
/// <returns>json字符串</returns>
public static string SerializeObject(object o)
{
string json = JsonConvert.SerializeObject(o);
return json;
} /// <summary>
/// 解析JSON字符串生成对象实体
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
/// <returns>对象实体</returns>
public static T DeserializeJsonToObject<T>(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
T t = o as T;
return t;
} /// <summary>
/// 解析JSON数组生成对象实体集合
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param>
/// <returns>对象实体集合</returns>
public static List<T> DeserializeJsonToList<T>(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
List<T> list = o as List<T>;
return list;
} /// <summary>
/// 反序列化JSON到给定的匿名对象.
/// </summary>
/// <typeparam name="T">匿名对象类型</typeparam>
/// <param name="json">json字符串</param>
/// <param name="anonymousTypeObject">匿名对象</param>
/// <returns>匿名对象</returns>
public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject)
{
T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
return t;
}
}
}

4.测试代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace NewTonJsonTest
{
class Program
{
static void Main(string[] args)
{
//将对象序列化为json字符串
Animal a = new Animal();
a.nickname = "xiaoxiao";
a.age = ; string s = JsonHelper.SerializeObject(a);
Console.WriteLine(s); //将json字符串反序列化为list<T>
string json = "[{\"nickname\":\"石子儿\",\"age\":\"110\"}]"; var list = JsonHelper.DeserializeJsonToList<Animal>(json);
Console.WriteLine(list[].nickname+"cc"); //将json 字符串序列化为对象 string jsonobj = "{\"nickname\":\"石子儿obj\",\"age\":\"110\"}";
var obj = JsonHelper.DeserializeJsonToObject<Animal>(jsonobj);
Console.WriteLine(obj.nickname); //将json 字符串转换成匿名对象
var tempEntity = new { nickname =string.Empty, age =};
string jsonany = "{\"nickname\":\"石子儿any\",\"age\":\"110\"}";
var objany = JsonHelper.DeserializeAnonymousType(jsonany,tempEntity);
Console.WriteLine(objany.nickname);
Console.ReadLine();
}
}
}

源码下载