1
note:
 {"category_id":"1","name":"Notes","icon":"images\/note.png"},
quote:
 {"category_id":"2","name":"Quotes","icon":"images\/quote.png"},
project:
 {"category_id":"3","name":"Projects","icon":"images\/project.png"},
skill:
 {"category_id":"4","name":"Skills","icon":"images\/skill.png"}

this is what is shown in my console. I have used json_decode for my query, and concatenated with string, and I believed that's the caused of the problem.

so how to turn string data into json format?

Frogmouth
  • 1,808
  • 1
  • 14
  • 22
user3189052
  • 75
  • 2
  • 9
  • 2
    I do not understand the problem itself. – Tigran Jan 27 '14 at 08:35
  • @Tigran assume the block of code is string, convert them into object – user3189052 Jan 27 '14 at 08:36
  • Well, you can not convert to an object. You can create object with parameters, some of them that can be from your string. As I see, you need 4 object where each has (id, name and link to icon), right? – Tigran Jan 27 '14 at 08:38
  • Have a look here - http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript – Tigran Jan 27 '14 at 08:43
  • or get back a JSON to your PHP you must change the content type: see this: http://stackoverflow.com/questions/267546/correct-content-type-http-header-for-json , but you must insert this string inside **{}** like: `{ note:{"cat ... skill.png"} }` – Frogmouth Jan 27 '14 at 08:46
  • however... why you use `json_decode`... it's trasnform a JSON string into a PHP variable (assoc. array), but for get back it to json for Javascript you may need to use `json_encode()` before. – Frogmouth Jan 27 '14 at 08:50

3 Answers3

5

If that's the string then I'll suggest to wrap it in { ... } and use `JSON.parse. I.e.:

var json = JSON.parse('{' + string + '}');

Of course you will need to add JSON lib helper to your page https://github.com/douglascrockford/JSON-js

Krasimir
  • 13,306
  • 3
  • 40
  • 55
0

you can use JSON.parse(string) this will return you a JSON from your string

jd.carretero
  • 139
  • 1
  • 1
  • 6
0

I think you have a problem with PHP not with Javascript:

you have decoded a JSON string for trasform it in a PHP object with json_decode work with it... but now you must get back it in a well format JSON string.

But first your string is invalid, for JSON standard (in PHP):

  1. Enclose your string in {
  2. the name of the properties note, quote, project, skill must be encapsuled inside " , your new string:

    { "note":{"category_id":"1","name":"Notes","icon":"images/note.png"}, "quote":{"category_id":"2","name":"Quotes","icon":"images/quote.png"}, "project"{"category_id":"3","name":"Projects","icon":"images/project.png"}, "skill":{"category_id":"4","name":"Skills","icon":"images/skill.png"} }

and now see this example of JSON encodingi in PHP:

$yourString = '{"note":{"category_id":"1","name":"Notes","icon":"images\/note.png"},"quote":{"category_id":"2","name":"Quotes","icon":"images\/quote.png"},"project":{"category_id":"3","name":"Projects","icon":"images\/project.png"},"skill":{"category_id":"4","name":"Skills","icon":"images\/skill.png"}}'; 
$JSON_FOR_PHP = json_decode($yourString);
$JSON_FOR_JS = json_encode($JSON_FOR_PHP);

/* response: */
echo "JSON for PHP (associative Array):<br><br>";
var_dump($JSON_FOR_PHP);
echo"<br><br>";
echo "JSON for JAVASCRIPT (JSON string {add content type: application/json}):<br><br>";
echo $JSON_FOR_JS;

response:

JSON for PHP (associative Array):

object(stdClass)#1 (4) { ["note"]=> object(stdClass)#2 (3) { ["category_id"]=> string(1) "1" ["name"]=> string(5) "Notes" ["icon"]=> string(15) "images/note.png" } ["quote"]=> object(stdClass)#3 (3) { ["category_id"]=> string(1) "2" ["name"]=> string(6) "Quotes" ["icon"]=> string(16) "images/quote.png" } ["project"]=> object(stdClass)#4 (3) { ["category_id"]=> string(1) "3" ["name"]=> string(8) "Projects" ["icon"]=> string(18) "images/project.png" } ["skill"]=> object(stdClass)#5 (3) { ["category_id"]=> string(1) "4" ["name"]=> string(6) "Skills" ["icon"]=> string(16) "images/skill.png" } }

JSON for JAVASCRIPT (JSON string {add content type: text/json}):

{"note":{"category_id":"1","name":"Notes","icon":"images\/note.png"},"quote":{"category_id":"2","name":"Quotes","icon":"images\/quote.png"},"project":{"category_id":"3","name":"Projects","icon":"images\/project.png"},"skill":{"category_id":"4","name":"Skills","icon":"images\/skill.png"}}

if you echo only $JSON_FOR_JS and change content type to application/json you get the response is a valid JSON string that you can parse with JSON.parse() in javascript:

header('Content-Type: application/json');
echo $JSON_FOR_JS;

or echo it directly in a JS script (html page or without script tag in .js file):

var js_json = JSON.parse();

now in javascript you have a object js_json with the content of your string.

Frogmouth
  • 1,808
  • 1
  • 14
  • 22