asp.net后台这么取json数据啊?求大神
比如{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u4e1c",...
比如{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u4e1c","area_id":"300000","region":"\u4e0a\u6d77\u5e02","region_id":"310000","city":"\u4e0a\u6d77\u5e02","city_id":"310000","county":"","county_id":"-1","isp":"\u7535\u4fe1","isp_id":"100017","ip":"180.168.169.114"}}
展开
展开全部
using System.Runtime.Serialization.Json;
using System.ServiceModel.Web;///记得引用这个命名空间
/// <summary>
/// 把JSON字符串还原为对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="szJson">JSON字符串</param>
/// <returns>对象实体</returns>
public static T ParseFormJson<T>(string szJson)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
return (T)dcj.ReadObject(ms);
}
}
更多追问追答
追问
请问这个怎么使用啊 哈哈 我菜鸟
追答
后台先从request里读取parameter的值,然后作为szJson参数变量调用ParseFormJson解析。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |