C# 解析Json

Mr_Evans 2010-11-16 02:22:21
0
0 由于对方提供的接口是Json,
以前没有做过这方面的解析
请各位大侠们看看怎么回事

下面是构建Json对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using System.Runtime.Serialization;


namespace WebTest {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
AjaxPro.Utility.RegisterTypeForAjax(typeof(WebTest._Default), this.Page);
//indApInfo a = new
}

[AjaxPro.AjaxMethod]
public string GetJson() {
List<indApInfo> list = new List<indApInfo>();
list.Add(new indApInfo { apName = "Name", addressContact = "四川", apType = 1, Email = "wansscncp@126.com", landLine = "xx", Mobile = "davds", postCode = "0" });
list.Add(new indApInfo { apName = "Name1", addressContact = "成都", apType = 2, Email = "fvdhsa@126.com", landLine = "xx", Mobile = "dsavdsa", postCode = "1" });
list.Add(new indApInfo { apName = "Name2", addressContact = "西昌", apType = 3, Email = "Emsda@126.com", landLine = "xx", Mobile = "138vds4", postCode = "2" });
list.Add(new indApInfo { apName = "Name3", addressContact = "南充", apType = 4, Email = "vdseee@126.com", landLine = "xx", Mobile = "vdavdsa", postCode = "3" });
string result = SerializatorJson.EntityToJson(new indApInfo { apName = "Name3", addressContact = "南充", apType = 4, Email = "vdseee@126.com", landLine = "xx", Mobile = "vdavdsa", postCode = "3" });
return result;
}

}



[DataContract]
public class indApInfo {
[DataMember]
public string apName { get; set; }
[DataMember]
public int apType { get; set; }
[DataMember]
public string addressContact { get; set; }
[DataMember]
public string postCode { get; set; }
[DataMember]
public string Mobile { get; set; }
[DataMember]
public string landLine { get; set; }
[DataMember]
public string Email { get; set; }
}
public static class SerializatorJson {
public static string EntityToJson<T>(T obj) where T : new() {
var ds = new DataContractJsonSerializer(typeof(T));
using (var ms = new MemoryStream()) {
ds.WriteObject(ms, obj);
return Encoding.UTF8.GetString(ms.ToArray());
}
}

public static T JsonToEntity<T>(string jsonStr) {
var ds = new DataContractJsonSerializer(typeof(T));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr));
var obj = (T)ds.ReadObject(ms);
ms.Close();
return obj;
}

}


}


这个是HTML调用 都没问题

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebTest._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script type="text/javascript">
function Start() {
var items = WebTest._Default.GetJson(); // 通过Ajax调用后台代码
var jstring = '(' + items.value + ')';
var j = eval(jstring);
for (var p in j) {
if (typeof (j[p]) == 'object') {
for (var pp in j[p]) {
alert(pp + ':' + j[p][pp]);
}

}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="Start()" value="调用" />
</div>
</form>
</body>
</html>



请问下 怎么在Winfrom中去解析这个Json对象
并且获得 List<indApInfo>这个集合。
...全文
307 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyerwing 2010-11-16
  • 打赏
  • 举报
回复
json的字符串呀.
Mr_Evans 2010-11-16
  • 打赏
  • 举报
回复

这个是JS请求获取数据的地方
var items = WebTest._Default.GetJson();

我在C# 应该怎么请求?
而且我是在WInfrom里面调用
用HttpRequest 去请求?
http://localhost:3739/
参数怎么加?
Mr_Evans 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 flyerwing 的回复:]
需要下载 Newtonsoft.Json.dll,并引用到项目中
2.0
public static object JsonToObject(string jsonString, object obj)
{
return JavaScriptConvert.DeserializeObject(jsonString, obj.GetType());
}
3.5
publi……
[/Quote]
jsonString 是什么?
flyerwing 2010-11-16
  • 打赏
  • 举报
回复
需要下载 Newtonsoft.Json.dll,并引用到项目中
2.0
public static object JsonToObject(string jsonString, object obj)
{
return JavaScriptConvert.DeserializeObject(jsonString, obj.GetType());
}
3.5
public static object JsonToObject(string jsonString, object obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
return serializer.ReadObject(mStream);
}
Mr_Evans 2010-11-16
  • 打赏
  • 举报
回复
别沉了。
有点急 帮忙解答下。
flyerwing 2010-11-16
  • 打赏
  • 举报
回复
jsontoobject
好象有这函数吧.

110,534

社区成员

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

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

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