Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"}]



I want to extract the parameters like author, articleTitle from this Object.

How do I go about it.

Ravindra
Posted
Updated 22-Aug-11 3:48am
v2

Hi I am getting this JSON object as webResponse when I call JSON servlet Interface in aspx page.

C#
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());
       }

   }


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
 
Share this answer
 
v2
Use JavaScriptSerializer to parse JSON objects.

Say

string json = yourjsonstring;


Now use :
JavaScriptSerializer ser = new JavaScriptSerializer();
var jsonobj= ser.Deserialize(json);



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:
 
Share this answer
 
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
 
Share this answer
 
hi, i want to know if you sold your problem actually i'm in the same case, i want to parse my response but i can't do it
 
Share this answer
 
How is a JSON object getting to your code behind ? You tagged this as C#, so I assume it's your code behind, and not your aspx, that is getting this. It would seem that if you can pass it via the client, so that it can be returned from the object created in a more readable manner, that wold be best. However, if you have no choice, you can use regex to parse this string for the data you need.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900