由于對方提供的接口是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 = "[email protected]", landLine = "xx", Mobile = "davds", postCode = "0" }); list.Add(new indApInfo { apName = "Name1", addressContact = "成都", apType = 2, Email = "[email protected]", landLine = "xx", Mobile = "dsavdsa", postCode = "1" }); list.Add(new indApInfo { apName = "Name2", addressContact = "西昌", apType = 3, Email = "[email protected]", landLine = "xx", Mobile = "138vds4", postCode = "2" }); list.Add(new indApInfo { apName = "Name3", addressContact = "南充", apType = 4, Email = "[email protected]", landLine = "xx", Mobile = "vdavdsa", postCode = "3" }); string result = SerializatorJson.EntityToJson(new indApInfo { apName = "Name3", addressContact = "南充", apType = 4, Email = "[email protected]", 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 回答
- 0 關注
- 514 瀏覽
添加回答
舉報
0/150
提交
取消