您的位置:首页 > Web前端 > JavaScript

C#解析Json(多方法解析Json 一)

2016-10-31 17:58 489 查看
解析:{'id':'4028d80858053bed0158053ef7a50001','sl':0.0,'sfyfz':'0','zwjyzsbh':'1000001600000018'}

1、新建winform控制台项目

2、在项目里新建一个实体类test.cs

class test
{

public string id { get; set; }
public string sl { get; set; }
public string sfyfz { get; set; }
public string zwjyzsbh { get; set; }

}


3、在Program.cs的Main方法里(方法一):  引用Newtonsoft.Json,反序列化

class Program
{
static void Main(string[] args)
{

string json=@"[{'id':'22222222','sl':0.0,'sfyfz':'0','zwjyzsbh':'333333333'}]";

List<test> jobInfoList = JsonConvert.DeserializeObject<List<test>>(json);
foreach (test jobInfo in jobInfoList)
{

Console.WriteLine("id:" + jobInfo.id);
}
Console.ReadLine();
}
}


string json这个字符串,里面都是单引号才可以,或者把单引号变为(\"),即斜杠+双引号,

string json=@"[{\"id\":\"22222222\",\"sl\":0.0,\"sfyfz\":\"0\",\"zwjyzsbh\":\"333333333\"}]";


要引用Newtonsoft.Json(nuget下载)



3、在Program.cs的Main方法里(方法二)  :JsonReader

class Program
{
static void Main(string[] args)
{

string json = @"[{'id':'4028d80858053bed0158053ef7a50001','sl':0.0,'sfyfz':'0','zwjyzsbh':'1000001600000018'}]";
JsonReader reader = new JsonTextReader(new StringReader(json));

while (reader.Read())
{
Console.WriteLine(reader.Value);
}
Console.ReadLine();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: