0

My web service is returning below mentioned output as JSON,

{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917}]}

But I doubt cause I am not able to parse it. If it is correct pls tell me how to parse it? I want to bind it to chart.

Full code

var retVal = '{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917},{"DailyTimeStamp":"Nov 02, 2013","Performance":119542}]}';
alert('Before parsing ' + retVal);
var passValue = JSON.parse(retVal);
alert('After parsing Count ' + passValue.count);

last alert give output as After parsing Count undefined.

AK47
  • 3,707
  • 3
  • 17
  • 36
  • Your json is fine @AK47. If you want to check, you can check your json at http://www.jsoneditoronline.org/ or jsonlint.com – AppleBud May 23 '14 at 10:48
  • Guys I want to parse in javascript , can u help me with code? assign this json string to varable ... – AK47 May 23 '14 at 10:50
  • Check this out http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript – AppleBud May 23 '14 at 10:51
  • yes tried that but it shows as undefined. pls check my question again I have edited – AK47 May 23 '14 at 10:58

5 Answers5

2

To tell if JSON is correct, you can use http://jsonlint.com/ Your JSON is correct. To parse it, use the .net JSON Deserializer http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx or JSON.NET Also see here for some discussion: Does .NET 4 have a built-in JSON serializer/deserializer?

EDIT: as you want Javascript, use JSON.parse. See some discussion here: http://www.json.org/js.html which also explains why you should not use eval(json), which does also work, but is considered a security risk.

Community
  • 1
  • 1
John Powell
  • 12,253
  • 6
  • 59
  • 67
2

The simplest way to parse your json in javascript is that you store it inside a variable and can proceed like this:

var jsonString=JSON.parse(yourJsonString);

then you can fetch each key and their value accordingly.

Just check your json on JsonEditoronline.org for a better way of reading this.

This is sample of how I read my json in javascript:

success:function(responseText)
                {
                     alert(responseText);
                    return false; 

                    var object= JSON.parse(responseText);
                    var response= object.response;
                    var div=$('<div></div>');
                    var docs= response.docs;

where my json was something like this:

{"response":{"docs":""}}

Hope it helps.

I parsed your json using javascript and was able to parse it using :

var myString='{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917}]}';

               var jsonString=JSON.parse(myString);
               alert("---------==="+jsonString);

               var fetch= jsonString.FetchSitePerformanceAutoResult;
               alert("llllllll    -- "+fetch.length);

               var time= fetch[0].DailyTimeStamp;
               alert(time);
AppleBud
  • 1,501
  • 8
  • 31
  • 47
  • OMG! Got it it took 2 complete days to understand me that there will be hierarchy in result object I was trying to skip "FetchSitePerformanceAutoResult". Anyway thanks a lot mate! – AK47 May 23 '14 at 11:16
  • Pleasure buddy. Thats why I asked you to check your json at jsoneditoronline because it displays your json in hierarchical format and gives you a better understanding of it. Happy coding.. :) – AppleBud May 23 '14 at 11:27
  • still after looking at JsonEditoronline.org I was not able to understand it. But after you shared code in detail (var fetch..) I understood the issue. Thanks again friend. – AK47 May 23 '14 at 12:01
1

Yes your json output is correct and you can check yourself here is the Link, this will help you convert your json to c# class. Hope this helps.

Vyas
  • 2,734
  • 18
  • 14
1

That is a valid JSON String. You can check it at http://jsonlint.com/

1

It is legal - php json_decode('{"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 01, 2013","Performance":106917}]}'); parses it fine.

websterridge
  • 369
  • 1
  • 5