C# 解析包含数组的 json字符串

版权声明:转载需注明出处 https://blog.csdn.net/w529409399/article/details/80116651

json 示例:

{
	"status":0,
	"message":"OK",
	"item":[
		{
			"id":54308113,
			"note_id":null,
			"call_start_at":"2018-04-18 13:07:02"
		},
		{
			"id":54308196,
			"note_id":null,
			"call_start_at":"2018-04-18 13:07:30"
		}
	],
	"size":2,
	"total":1,
	"total_pages":2
}

试了 fastjson,但是对于json中的数组item解析不了,会报错。然后换成了Newtonsoft.Json ,解决,数组转成了List。

引用Newtonsoft.Json.dll

using Newtonsoft.Json;
using System.IO;
using System.Collections.Generic;
private void JsonToList(string jsonstr)
     {
            byte[] array = Encoding.UTF8.GetBytes(jsonstr);
            MemoryStream stream = new MemoryStream(array);             //convert stream 2 string      
            StreamReader streamReader = new StreamReader(stream);
            JsonSerializer serializer = new JsonSerializer();
            callLogMessage p1 = (callLogMessage)serializer.Deserialize(new JsonTextReader(streamReader), typeof(callLogMessage));
            streamReader.Dispose();
            List<calllog> calllogs = p1.item.ToList<calllog>(); //数组转成List
     }

json 对应 callLogMessage实体类。

public class callLogMessage
    {
        public int status;
        public string message;
        public calllog[] item;
        public int size;
        public int total;
        public int total_pages;
    }

json 中的数组对应 List<calllog>

public class calllog
    {
        public string id { get; set; }
        public string note_id { get; set; }
        public string call_start_at { get; set; }
    }



猜你喜欢

转载自blog.csdn.net/w529409399/article/details/80116651