json字符串:
{
"baseInfo" : {
"id" : "370171000bd8a85d144e6bbf9bb741aaeb307a",
"name" : "王尼玛",
"addr" : null,
"presentAddressCounty" : "历下区",
"presentAddressTown" : "建筑新村街道办事处",
"presentAddressVillage" : "历山东路居委会",
"presentAddressHouseNum" : "历山东路居委会历山东路2019号11-21-10",
"isDisease" : "1",
"isTrauma" : null,
"isOperration" : null,
"isTransfusion" : null,
"idNo" : null
},
"EhrPirDisease" : [ {
"id" : null,
"pirId" : null,
"healthRecordCode" : null,
"diseaseCode" : null,
"diseaseName" : "冠心病",
"diagnoseTime" : null
}, {
"id" : null,
"pirId" : null,
"healthRecordCode" : null,
"diseaseCode" : null,
"diseaseName" : "高血压",
"diagnoseTime" : null
} ]
}
利用C#处理这个字符串,在控制台显示出人员一些信息
解决步骤:
- 在VS中新建项目——控制台应用程序。
- 引用类库:在VS中点击:项目-》引用-》扩展-》找到Json.Net(Newtonsoft.Json.dll)-》勾选-》确定。
- 没有就下载对应的版本:http://json.codeplex.com/然后继续上一步
- 在主程序Program.cs的开头添加 using Newtonsoft.Json; 这一语句。
- 把要处理的json字符串复制到VS里面直接选择 编辑-》选择性粘贴-》将Json粘贴为类得到C#类:
public class BaseInfo
{
public string id { get; set; }
public string name { get; set; }
public string addr { get; set; }
public string presentAddressCounty { get; set; }
public string presentAddressTown { get; set; }
public string presentAddressVillage { get; set; }
public string presentAddressHouseNum { get; set; }
public string isDisease { get; set; }
public string isTrauma { get; set; }
public string isOperration { get; set; }
public string isTransfusion { get; set; }
public string idNo { get; set; }
}
public class EhrPirDisease
{
public string id { get; set; }
public string pirId { get; set; }
public string healthRecordCode { get; set; }
public string diseaseCode { get; set; }
public string diseaseName { get; set; }
public string diagnoseTime { get; set; }
}
public class RootObject
{
public BaseInfo baseInfo { get; set; }
public List<EhrPirDisease> EhrPirDisease { get; set; }
}
把这部分内容放到class Test类的下方。
- json中有很多的引号,赋值给一个string变量还要加上很多的反斜杠。这里给大家一个在线工具:http://tool.chinaz.com/tools/jsonescape.aspx将Json代码复制进去点击“压缩并转义”,得到的值可以直接赋值给一个string变量,如图:
string jsonText = "{\"baseInfo\":{\"id\":\"370171000bd8a85d144e6bbf9bb741aaeb307a\",\"name\":\"王尼玛\",\"addr\":null,\"presentAddressCounty\":\"历下区\",\"presentAddressTown\":\"建筑新村街道办事处\",\"presentAddressVillage\":\"历山东路居委会\",\"presentAddressHouseNum\":\"历山东路居委会历山东路2019号11-21-10\",\"isDisease\":\"1\",\"isTrauma\":null,\"isOperration\":null,\"isTransfusion\":null,\"idNo\":null},\"EhrPirDisease\":[{\"id\":null,\"pirId\":null,\"healthRecordCode\":null,\"diseaseCode\":null,\"diseaseName\":\"冠心病\",\"diagnoseTime\":null},{\"id\":null,\"pirId\":null,\"healthRecordCode\":null,\"diseaseCode\":null,\"diseaseName\":\"高血压\",\"diagnoseTime\":null}]}";
- 反序列化json字符串得到对象。
RootObject rb = JsonConvert.DeserializeObject<RootObject>(jsonText);
- 处理对象,如输出人员姓名:
Console.WriteLine("姓名:"+rb.baseInfo.name);
- 完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace JsonTest
{
public class Test
{
static void Main(string[] args)
{
string jsonText = "{\"baseInfo\":{\"id\":\"370171000bd8a85d144e6bbf9bb741aaeb307a\",\"name\":\"王尼玛\",\"addr\":null,\"presentAddressCounty\":\"历下区\",\"presentAddressTown\":\"建筑新村街道办事处\",\"presentAddressVillage\":\"历山东路居委会\",\"presentAddressHouseNum\":\"历山东路居委会历山东路2019号11-21-10\",\"isDisease\":\"1\",\"isTrauma\":null,\"isOperration\":null,\"isTransfusion\":null,\"idNo\":null},\"EhrPirDisease\":[{\"id\":null,\"pirId\":null,\"healthRecordCode\":null,\"diseaseCode\":null,\"diseaseName\":\"冠心病\",\"diagnoseTime\":null},{\"id\":null,\"pirId\":null,\"healthRecordCode\":null,\"diseaseCode\":null,\"diseaseName\":\"高血压\",\"diagnoseTime\":null}]}";
Console.WriteLine(jsonText);
RootObject rb = JsonConvert.DeserializeObject<RootObject>(jsonText);
Console.WriteLine("姓名:"+rb.baseInfo.name);
Console.WriteLine("家庭住址:"+rb.baseInfo.presentAddressHouseNum);
foreach (EhrPirDisease ep in rb.EhrPirDisease)
{
Console.WriteLine("既往病史:"+ep.diseaseName);
}
//Console.ReadKey();
}
public class BaseInfo
{
public string id { get; set; }
public string name { get; set; }
public string addr { get; set; }
public string presentAddressCounty { get; set; }
public string presentAddressTown { get; set; }
public string presentAddressVillage { get; set; }
public string presentAddressHouseNum { get; set; }
public string isDisease { get; set; }
public string isTrauma { get; set; }
public string isOperration { get; set; }
public string isTransfusion { get; set; }
public string idNo { get; set; }
}
public class EhrPirDisease
{
public string id { get; set; }
public string pirId { get; set; }
public string healthRecordCode { get; set; }
public string diseaseCode { get; set; }
public string diseaseName { get; set; }
public string diagnoseTime { get; set; }
}
public class RootObject
{
public BaseInfo baseInfo { get; set; }
public List<EhrPirDisease> EhrPirDisease { get; set; }
}
}
}
- 对于json字符串中的数组,如本例中的EhrPirDiseas,用这篇文章里的在线工具会自动生成泛型列表List。可以直接把它当成数组使用。遍历数组的方法在此依然适用,例如代码中的:
foreach (EhrPirDisease ep in rb.EhrPirDisease)
{
Console.WriteLine("既往病史:"+ep.diseaseName);
}