I am currently working on a php/html/javascript project and am doing ajax post to a php script which returns json encoded data. Within this json encoded there is some more json encoded data.
Below is how I am json encoding the data
$category[0]['Category'] = "Category 1";
$category[1]['Category'] = "Category 2";
$article['Date'] = "11/11/2012 22:42:00";
$article['Title'] = "This is the title";
$article['Subtitle'] = "This is the subtitle";
$article['Content'] = "This is the content1";
$article['ViaName'] = "SomeSite";
$article['ViaAddress'] = "http://localhost";
$article['SourceName'] = "N/A";
$article['SourceAddress'] = "N/A";
$article['categories'] = json_encode($category);
echo json_encode($article);
The $article that gets json encoded I can access fine when it is returned to the javascript using json.Title, json.Subtitle etc.
But when I try and get the categories it doesn’t work.
I’ve tried using json.categories and this prints out the following:
[{"Category":"Category 1"},{"Category":"Category 2"}]
This looks like its working so when I try and access each individual part it then doesn’t work. I’ve tried json.categories[0].Category and json.categories.Category[0] but it keeps on coming up an undefined.
Thanks for any help you can provide.
If you encode an array with
json_encodeyou got astring. So, in your case,json.categoriesis a string and not an array.However,
json_encode(and decode) is recursive.So you should use simply
without encode again.