为了账号安全,请及时绑定邮箱和手机立即绑定

C# 解析Json

C# 解析Json

呼啦一阵风 2018-12-06 15:32:57
由于对方提供的接口是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>这个集合。  
查看完整描述

1 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

试试这个:Newtonsoft.Json

查看完整回答
反对 回复 2019-01-21
  • 1 回答
  • 0 关注
  • 504 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信