C# winform 怎么解析接收到的JSON数据?

ld8530056 2010-08-25 06:41:26
{"total":1,"root":[{"birthday":"1987-09-09","sex":"男","post":"611756","fax":"1234565","education":"4","mobilephone":"13194879680","rank":"经理","email":"1121@qq.com","address":"四川成都交大","idcode":"510502199001200014","name":"李镀","account":"002","branch":"监察科","telephone":"0830-2396906"}]}

以上是我接收到的数据。现在问题是怎么把他们解析并在winform中显示出来
...全文
1349 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
宝_爸 2010-08-26
  • 打赏
  • 举报
回复
net提供了类库:


using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;


[DataContract]
public class DataContruct
{
[DataMember]
internal string total;

[DataMember]
IEnumerable<rootData> root;
}

[DataContract]
public class rootData
{
[DataMember]
internal string birthday;

[DataMember]
internal string sex;

[DataMember]
internal string post;

[DataMember]
internal string fax;

[DataMember]
internal string education;

[DataMember]
internal string mobilephone;

[DataMember]
internal string rank;


[DataMember]
internal string email;


[DataMember]
internal string address;


[DataMember]
internal string idcode;

[DataMember]
internal string name;


[DataMember]
internal string account;


[DataMember]
internal string branch;


[DataMember]
internal string telephone;

}

....
string testdata = "{\"total\":1,\"root\":[{\"birthday\":\"1987-09-09\",\"sex\":\" 男\",\"post\":\"611756\",\"fax\":\"1234565\",\"education\":\"4\",\"mobilephone\":\"13194879680\",\"rank\":\" 经理\",\"email\":\"1121@qq.com\",\"address\":\"四川成都交大\",\"idcode\":\"510502199001200014\",\"name\":\"李镀\",\"account\":\"002\",\"branch\":\"监察科\",\"telephone\":\"0830-2396906\"}]}";

MemoryStream stream2 = new MemoryStream();
DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(DataContruct));
StreamWriter wr = new StreamWriter(stream2);
wr.Write(testdata);
wr.Flush();
stream2.Position = 0;
Object obj = ser2.ReadObject(stream2);
DataContruct p2 = (DataContruct)obj;
....

路遥迢 2010-08-25
  • 打赏
  • 举报
回复
赚赚
/// <summary>
/// Dictionary<string, string> 转实体自动赋值
/// </summary>
/// <param name="row">JSON转的Dictionary<string, string>[]</param>
/// <param name="targetObj">实体object</param>
/// <returns>实体object</returns>
public object ChangeToObject(string json, object targetObj)
{
if (json != "" || json != null || targetObj != null)
{
Dictionary<string, string>[] companies = JSON.Deserialize<Dictionary<string, string>[]>(json);

PropertyInfo[] propinfos = propinfos = targetObj.GetType().GetProperties();

for (int i = 0; i < propinfos.Length; i++)
{
foreach (var item in companies[0])
{
if (item.Key == propinfos[i].Name)
{
//如果是nullable类型则需要以其他方法获取类型
if (propinfos[i].PropertyType.IsGenericType && propinfos[i].PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
//类型转换
object ValueA = Convert.ChangeType(item.Value, propinfos[i].PropertyType.GetGenericArguments()[0]);
//自动匹配赋值
propinfos[i].SetValue(targetObj, ValueA, null);
}
else
{
object ValueB = Convert.ChangeType(item.Value, propinfos[i].PropertyType);
propinfos[i].SetValue(targetObj, ValueB, null);
}
break;
}
}
}
return targetObj;
}
return null;
}
鸭梨山大帝 2010-08-25
  • 打赏
  • 举报
回复
http://james.newtonking.com/pages/json-net.aspx

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧