解析aspx中的JSON对象 [英] Parse JSON Object in aspx
问题描述
我有如下的JSON对象.
I have JSON Object as below.
[{"articleLeader":"","articlePostDate":{"date":12,"day":4,"hours":0,"minutes":0,"month":10,"seconds":1,"time":1258012801000,"timezoneOffset":480,"year":109},"articleTitle":"AZ United Care Home LLC","author":"","webPageFileName":"AZ_United_Care_Home.html","webPageURI":"/AZ_United_Care_Home.html"},
{"articleLeader":"","articlePostDate":{"date":12,"day":4,"hours":0,"minutes":0,"month":10,"seconds":1,"time":1258012801000,"timezoneOffset":480,"year":109},"articleTitle":"Baldris Home Care","author":"","webPageFileName":"Baldris_Home_Care.html","webPageURI":"/Baldris_Home_Care.html"},
{"articleLeader":"","articlePostDate":{"date":12,"day":4,"hours":0,"minutes":0,"month":10,"seconds":1,"time":1258012801000,"timezoneOffset":480,"year":109},"articleTitle":"Basia Residential Care","author":"","webPageFileName":"Basia_Residential_Care.html","webPageURI":"/Basia_Residential_Care.html"}]
我想从该对象中提取诸如author,articleTitle之类的参数.
我该怎么办.
Ravindra
I want to extract the parameters like author, articleTitle from this Object.
How do I go about it.
Ravindra
推荐答案
当我在aspx页面中调用JSON servlet接口时,我将这个JSON对象作为webResponse获得.
Hi I am getting this JSON object as webResponse when I call JSON servlet Interface in aspx page.
try
{
string URL = "http://xapi-content.jbossdev/json";
string content = "service=contentService&method=getArticlePagesByService¶ms=<a class="\"array\""><e type="\"string\"" key="\"webSiteName\"">www.testecl.com</e><e type="\"string\"" key="\"serviceCode\"">Senior_Home_Care</e><e type="\"string\"" key="\"maxCount\"">3</e><e type="\"string\"" key="\"sortOrder\"">ARTICLE_SORT_BY_TITLE_ACENDING</e></a>";
//Console.WriteLine(URL);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL);
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.Method = "POST";
webRequest.ProtocolVersion = HttpVersion.Version11;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = content.Length;
Stream requestStream = webRequest.GetRequestStream();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] array = encoding.GetBytes(content);
requestStream.Write(array, 0, array.Length);
requestStream.Flush();
requestStream.Close();
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
{
jsonResponse = sr.ReadToEnd();
}
Response.Write(jsonResponse);
}
catch (Exception objExp)
{
Console.WriteLine(objExp.ToString());
}
}
所以现在我在``jsonResponse''字符串变量中有了这个JSON字符串.
我只是aspx开发的初学者,这是我的第一个项目.
我已经从http://json.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=37810
下载了Json.NET 3.5 Release 6
现在我不知道如何实现此库并解析JSON字符串.
请指导.
Ravindra
So Now I have this JSON string in my ''jsonResponse'' string variable.
I am just starter in aspx devlopment, this is my first project.
I have downloaded Json.NET 3.5 Release 6 from http://json.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=37810
Now I do not know How to implement this library and parse JSON string.
Please guide.
Ravindra
使用JavaScriptSerializer
解析JSON对象.
说
UseJavaScriptSerializer
to parse JSON objects.
Say
string json = yourjsonstring;
现在使用:
Now use :
JavaScriptSerializer ser = new JavaScriptSerializer();
var jsonobj= ser.Deserialize(json);
只需将yourjsonscript放在字符串变量中,然后将其反序列化为您自己的对象即可.您也可以定义一个类而不是使用匿名类型. :cool:
Just place yourjsonscript in the string variable and Deserialize it to your own object. You can also define a class rather than using an annonymous type. :cool:
它给了我下面的错误.
错误1找不到类型或名称空间名称"JavaScriptSerializer"(您是否缺少using指令或程序集引用?)C:\ ElderCare \ ArticleData.aspx.cs
错误3找不到类型或名称空间名称"var"(您是否缺少using指令或程序集引用?)C:\ ElderCare \ ArticleData.aspx.cs 74 13 C:\ ElderCare \
我不明白如何将jsonscript放入字符串变量并将其反序列化为您的对象
Hi It gave me below error.
Error 1 The type or namespace name ''JavaScriptSerializer'' could not be found (are you missing a using directive or an assembly reference?) C:\ElderCare\ArticleData.aspx.cs
Error 3 The type or namespace name ''var'' could not be found (are you missing a using directive or an assembly reference?) C:\ElderCare\ArticleData.aspx.cs 74 13 C:\ElderCare\
I did not understand How do I place jsonscript in the string variable and Deserialize it to your my object
这篇关于解析aspx中的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!