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?

6
  • 2
    I do not understand the problem itself. Commented Jan 27, 2014 at 8:35
  • @Tigran assume the block of code is string, convert them into object Commented Jan 27, 2014 at 8: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? Commented Jan 27, 2014 at 8:38
  • Have a look here - stackoverflow.com/questions/4935632/… Commented Jan 27, 2014 at 8:43
  • or get back a JSON to your PHP you must change the content type: see this: stackoverflow.com/questions/267546/… , but you must insert this string inside {} like: { note:{"cat ... skill.png"} } Commented Jan 27, 2014 at 8:46

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

can't it be done in php? I wish not to include extra stuff.
+1 for { add... but i think @user3189052 has other problems before JS string parsing.
USE: json_encode() to a VALID json string.
Yes, you can use $string = '{'.$string.'}'; and then send the string to the client-side.
not only... he must add double quotes to properties name (skills, quote,note,project).
0

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

1 Comment

have you try to do that before post this answer?
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.