1.根据JSON文件格式,建立实体类,例如根据json217.txt建立实体类Magic,包含id、name、number三个字段以及一个方法 2.根据json格式,新建list,使用Magic实体类作为泛型,新建magicList类 List<Magic> magicList = new List<Magic>(); 3.通过JsonMapper转为对象(jsondata),赋值到magicList中 4.对magicList进行遍历输出
新建一个Magic实体类用于解析
Magic.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace json操作
{
class Magic
{
public int id;
public string name;
public int number;
public override string ToString()
{
return string.Format("id:{0},name:{1},numebr:{2}",id,name,number);
}
}
}
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引入LitJson
using LitJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace json操作
{
class Program
{
static void Main(string[] args)
{
//因为json文件为集合,所以新建一个集合
List<Magic> magicList = new List<Magic>();
//依旧使用JsonMapper去进行解析
JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("json217.txt"));
foreach (JsonData temp in jsonData)
{
//创建一个新对象
Magic magic = new Magic();
//通过索引其去取得temp中的value
//返回值还是jsondata类型
JsonData idvalue = temp["id"];
JsonData namevalue = temp["name"];
JsonData numbervalue = temp["number"];
//将jsondata转化为字符串进行输出
//name本身就为str,所以不用转换
int id = Int32.Parse(idvalue.ToString());
int number = Int32.Parse(numbervalue.ToString());
magic.id = id;
magic.number = number;
magic.name = namevalue.ToString();
magicList.Add(magic);
}
foreach (var temp in magicList)
{
Console.WriteLine(temp);
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引入LitJson
using LitJson;
namespace json操作
{
class Program
{
static void Main(string[] args)
{
//任何可以数组的地方都可以使用一个集合
List<Magic> magicklist = JsonMapper.ToObject<List<Magic>>(File.ReadAllText("json217.txt"));
foreach (var temp in magicklist)
{
Console.WriteLine(temp);
}
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace json操作
{
public class DevicelistItem
{
public string productcode { get; set; }
public string devicecode { get; set; }
public string url { get; set; }
public override string ToString()
{
return string.Format("productcode:{0},devicecode:{1},url:{2}", productcode, devicecode, url);
}
}
public class IotDevice
{
public int status { get; set; }
public string apiId { get; set; }
public string date { get; set; }
public string message { get; set; }
public List<DevicelistItem> devicelist { get; set; }
public override string ToString()
{
return string.Format("status:{0},apiId:{1},date:{2},message:{3},devicelist:{4},", status, apiId, date, message, devicelist);
}
}
}
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引入LitJson
using LitJson;
namespace _032json操作
{
class Program
{
static void Main(string[] args)
{
//json文档整体为IotDevice类型,所以转换的时候泛型为<IotDevice>
//返回值为IotDevice对象
IotDevice iotdevice = JsonMapper.ToObject <IotDevice>(File.ReadAllText("TextFile1.txt"));
Console.WriteLine(iotdevice);
foreach (var temp in iotdevice.devicelist)
{
Console.WriteLine(temp);
}
Console.Read();
}
}
}
运行结果:
5.4复杂的JSON文件解析
原始json内容
TextFlie2.txt:
在这里插入代码片
六、字符串转化为JSON
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引入LitJson
using LitJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace json操作
{
class Program
{
static void Main(string[] args)
{
//新建IotDevice的实体类
IotDevice device1 = new IotDevice();
device1.date = "2022/02/17";
device1.apiId = "89757";
//转为json的字符串
string json = JsonMapper.ToJson(device1);
Console.WriteLine(json);
Console.Read();
}
}
}
1.根据JSON文件格式,建立实体类,例如根据json217.txt建立实体类Magic,包含id、name、number三个字段以及一个方法 2.根据json格式,新建list,使用Magic实体类作为泛型,新建magicList类 List<Magic> magicList = new List<Magic>(); 3.通过JsonMapper转为对象(jsondata),赋值到magicList中 4.对magicList进行遍历输出
新建一个Magic实体类用于解析
Magic.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace json操作
{
class Magic
{
public int id;
public string name;
public int number;
public override string ToString()
{
return string.Format("id:{0},name:{1},numebr:{2}",id,name,number);
}
}
}
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引入LitJson
using LitJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace json操作
{
class Program
{
static void Main(string[] args)
{
//因为json文件为集合,所以新建一个集合
List<Magic> magicList = new List<Magic>();
//依旧使用JsonMapper去进行解析
JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("json217.txt"));
foreach (JsonData temp in jsonData)
{
//创建一个新对象
Magic magic = new Magic();
//通过索引其去取得temp中的value
//返回值还是jsondata类型
JsonData idvalue = temp["id"];
JsonData namevalue = temp["name"];
JsonData numbervalue = temp["number"];
//将jsondata转化为字符串进行输出
//name本身就为str,所以不用转换
int id = Int32.Parse(idvalue.ToString());
int number = Int32.Parse(numbervalue.ToString());
magic.id = id;
magic.number = number;
magic.name = namevalue.ToString();
magicList.Add(magic);
}
foreach (var temp in magicList)
{
Console.WriteLine(temp);
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引入LitJson
using LitJson;
namespace json操作
{
class Program
{
static void Main(string[] args)
{
//任何可以数组的地方都可以使用一个集合
List<Magic> magicklist = JsonMapper.ToObject<List<Magic>>(File.ReadAllText("json217.txt"));
foreach (var temp in magicklist)
{
Console.WriteLine(temp);
}
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace json操作
{
public class DevicelistItem
{
public string productcode { get; set; }
public string devicecode { get; set; }
public string url { get; set; }
public override string ToString()
{
return string.Format("productcode:{0},devicecode:{1},url:{2}", productcode, devicecode, url);
}
}
public class IotDevice
{
public int status { get; set; }
public string apiId { get; set; }
public string date { get; set; }
public string message { get; set; }
public List<DevicelistItem> devicelist { get; set; }
public override string ToString()
{
return string.Format("status:{0},apiId:{1},date:{2},message:{3},devicelist:{4},", status, apiId, date, message, devicelist);
}
}
}
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引入LitJson
using LitJson;
namespace _032json操作
{
class Program
{
static void Main(string[] args)
{
//json文档整体为IotDevice类型,所以转换的时候泛型为<IotDevice>
//返回值为IotDevice对象
IotDevice iotdevice = JsonMapper.ToObject <IotDevice>(File.ReadAllText("TextFile1.txt"));
Console.WriteLine(iotdevice);
foreach (var temp in iotdevice.devicelist)
{
Console.WriteLine(temp);
}
Console.Read();
}
}
}
运行结果:
5.4复杂的JSON文件解析
原始json内容
TextFlie2.txt:
在这里插入代码片
六、字符串转化为JSON
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引入LitJson
using LitJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace json操作
{
class Program
{
static void Main(string[] args)
{
//新建IotDevice的实体类
IotDevice device1 = new IotDevice();
device1.date = "2022/02/17";
device1.apiId = "89757";
//转为json的字符串
string json = JsonMapper.ToJson(device1);
Console.WriteLine(json);
Console.Read();
}
}
}