Case 1: What does not work for me?
$phpObj=json_decode(file_get_contents($url),true);
In above file_get_contents returns me a json, I convert that to php object using above and then I parse $phpObj (to extract certain strings) which is a multidimensional array and just form a new array of key value say $newObj. After that I simply convert newObj to json using json_encode.
I use jQuery to parse the $newObj and it renders it to html.
Case 2: What does work for me?
Instead of parsing json I get my server to return XML and instead of json_decode like above I use:
$xmlDoc->load($url);
I parse the XML create the $newObj (key value pair) & then I use jQuery same like above which renders the recordset on browser. Works perfectly fine.
Now the only difference between case 1 and 2 ofcourse other than parsiong JSON and XML is the value that jQuery receives in the callback function for rendering:
For Case 1 (does not work):
{"1002":"I am Yavar","1003":"I work for XYZ","1004":"California is in US"}
For Case 2 (works):
[{"1000":"California is in US","xmlNode":{}},{"1001":"I work for XYZ","xmlNode":{}}]
It would be great if somebody could help me as to what is going wrong in case 1 and are the square brackets ([]) and xmlNode stuff coming in case 2 really required to make it work?
If really required here is my jQuery function:
<script>
$(document).ready( function() {
$('#term').keyup( function() {
$.get('search_json.php?q='+escape($('#term').val()), function(data) {
html = '<table id="results">';
$.each( data, function( ind, ep ) {
html += '<tr><td class="document"><b>'+ind.key+'</b> ';
html += +ep.value+'</td></tr>';
} );
html += '</html>';
$('#results').replaceWith( html );
} );
} );
} );
</script>