JSON/XML parsing & jQuery callback for processing output json

jesusazogue

New Member
Case 1: What does not work for me?\[code\]$phpObj=json_decode(file_get_contents($url),true);\[/code\]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:\[code\]$xmlDoc->load($url);\[/code\]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):\[code\]{"1002":"I am Yavar","1003":"I work for XYZ","1004":"California is in US"}\[/code\]For Case 2 (works):\[code\][{"1000":"California is in US","xmlNode":{}},{"1001":"I work for XYZ","xmlNode":{}}]\[/code\]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:\[code\]<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>&nbsp;'; html += +ep.value+'</td></tr>'; } ); html += '</html>'; $('#results').replaceWith( html ); } );} );} );</script>\[/code\]
 
Back
Top