public class Person
{
public String FirstName
{ get; set; }
public String LastName
{ get; set; }
public int Age
{ get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace JsonDemo
{
class Program
{
static void Main(string[] args)
{
string personJson = "{ 'FirstName': '小坦克','LastName':'Tank xiao', 'Age':'30', 'Books':[{'BookName':'c#', 'Price':'29.9'},{'BookName':'Mac编程', 'Price':'39.9'}]}";
string allMoveJson = @"[{ 'FirstName': '小坦克','LastName':'Tank xiao', 'Age':'30', 'Books':[{'BookName':'c#', 'Price':'29.9'},{'BookName':'Mac编程', 'Price':'39.9'}]},{
'FirstName': '小坦克2','LastName':'Tank xiao2', 'Age':'40', 'Books':[{'BookName':'c#', 'Price':'29.9'},{'BookName':'Mac编程', 'Price':'39.9'}]}]";
// 反序列化 单个对象
Person oneMovie = JsonConvert.DeserializeObject<Person>(personJson);
// 反序列化 对象集合
List<Person> allMovie = JsonConvert.DeserializeObject<List<Person>>(allMoveJson);
Console.WriteLine(oneMovie.FirstName);
Console.WriteLine(allMovie[1].FirstName);
// 序列化
string afterJson = JsonConvert.SerializeObject(allMovie);
}
}
public class Person
{
public String FirstName
{ get; set; }
public String LastName
{ get; set; }
public int Age
{ get; set; }
public List<Book> Books
{ get; set; }
}
public class Book
{
public string BookName
{ get; set; }
public string Price
{ get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
namespace TestJson
{
class Program
{
static void Main(string[] args)
{
string json = "{\"a\":\"sadsad\",\"c\":{\"d\":\"adad\"}}";
//CCC ccc = new CCC();
//ccc.d = "adad";
//AAA aaa = new AAA();
//aaa.a = "sadsad";
//aaa.b = "dfgdg";
//aaa.c = ccc;
AAA aaa = new AAA();
aaa= JavaScriptConvert.DeserializeObject<AAA>(json);
// string json = JavaScriptConvert.SerializeObject(aaa);
// Console.WriteLine(json);
Console.Read();
}
}
public class AAA {
private string m_a;
public string a
{
get { return m_a; }
set { m_a = value; }
}
private string m_b;
public string b
{
get { return m_b; }
set { m_b = value; }
}
private CCC m_c;
public CCC c
{
get { return m_c; }
set { m_c = value; }
}
}
public class CCC
{
private string m_d;
public string d
{
get { return m_d; }
set { m_d = value; }
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有