Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why parseJSON returns null

Tags:

json

jquery

I have following jQuery/JS code

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script>
  $.getJSON("http://example/topics", {}, function(data){
           console.log( data ); // display the JSON *data* in the web console
           console.log( $.parseJSON(data) );
  });
</script>

console.log(data) shows JSON data within firebug console (firefox add-on), but console.log( $.parseJSON(data) ) shows null.

what could be possible reason.

I need to convert JSON string into array.

like image 644
I-M-JM Avatar asked Jun 24 '11 08:06

I-M-JM


People also ask

Is empty string a valid JSON?

Empty ValuesNull values and empty strings (“”) are valid JSON values, but the state of being undefined is not valid within JSON.


1 Answers

I assume because $.getJSON already parsed the data. data is Javascript object.

The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method.

Of course we could say more if you show us the output, but normally, $.getJSON already parses the response.

P.S.: I hope you use a proper URL in your real code.

like image 114
Felix Kling Avatar answered Oct 07 '22 12:10

Felix Kling