Caio Mar
Caio Mar

Reputation: 2624

What is wrong with this JSON string?

I keep getting NULL when i do $data = json_decode($data); . To encode it in JS I use var data = JSON.stringify( my_obj ); What is wrong with this JSON string?

 'guide' => string (1583) "[{\"instructions\":[\"sdasda\"],\"media\":[{\"id\":95,\"titl …"

EDIT:

I am adding the entire string as requested:

[{\"instructions\":[\"sdasda\"],\"media\":[{\"id\":95,\"title\":\"item4_1444\",\"filename\":\"item4_1444.jpg\",\"url\":\"http://localhost:8888/sandbox/content/uploads/2014/08/item4_1444.jpg\",\"link\":\"http://localhost:8888/sandbox/example/change-phone-home-button/item4_1444/\",\"alt\":\"\",\"author\":\"1\",\"description\":\"\",\"caption\":\"\",\"name\":\"item4_1444\",\"status\":\"inherit\",\"uploadedTo\":86,\"date\":\"2014-08-24T16:39:15.000Z\",\"modified\":\"2014-08-24T16:39:15.000Z\",\"menuOrder\":0,\"mime\":\"image/jpeg\",\"type\":\"image\",\"subtype\":\"jpeg\",\"icon\":\"http://localhost:8888/sandbox/includes/images/media/default.png\",\"dateFormatted\":\"24/08/2014\",\"nonces\":{\"update\":\"46bbaaed37\",\"delete\":\"bcda3390c5\",\"edit\":\"ea35506ec2\"},\"editLink\":\"http://localhost:8888/sandbox/admin/post.php?post=95&action=edit\",\"sizes\":{\"thumbnail\":{\"height\":150,\"width\":150,\"url\":\"http://localhost:8888/sandbox/content/uploads/2014/08/item4_1444-150x150.jpg\",\"orientation\":\"landscape\"},\"medium\":{\"height\":300,\"width\":200,\"url\":\"http://localhost:8888/sandbox/content/uploads/2014/08/item4_1444-200x300.jpg\",\"orientation\":\"portrait\"},\"full\":{\"url\":\"http://localhost:8888/sandbox/content/uploads/2014/08/item4_1444.jpg\",\"height\":960,\"width\":640,\"orientation\":\"portrait\"}},\"height\":960,\"width\":640,\"orientation\":\"portrait\",\"compat\":{\"item\":\"\",\"meta\":\"\"},\"html_id\":\"ge-step-image-1\"}]}]

Upvotes: 1

Views: 611

Answers (1)

Deltics
Deltics

Reputation: 23036

There is nothing wrong with the JSON other than the fact that the double-quotes that are supposed to delimit string values in the JSON have been encoded as if they were double-quotes within a JSON string.

For example:

\"instructions\":

Should be simply:

"instructions":

To illustrate how double-quote escaping is intended to work in JSON, consider encoding an object with a single item named height with the string value 6'2":

height: 6'2"

Encoded in JSON this would be:

{ "height": "6'2\"" }

The JSON strings are delimited with double-quotes. The double-quote internal to the string value itself is then escaped using the backslash character.

Supplementary to this of course is how you encode your string for your given source code language.

If you are using a literal string in your source which is itself double-quote delimited or places special interpretation on '\' then you must adopt the conventions required by the source language in encoding your literals.

Again, by way of an example, consider how the above string would be represented in two different languages:

// C# 
s = "{ \"height\": \"6'2\\\"\" }";

// Delphi
s := '{ "height": "6''2\"" }';

In C# since '\' marks an escaped character then the '\' in the JSON must itself be escaped. The double quote characters also need to be escaped since C# string literals are also double-quote delimited.

In Delphi, string literals are single quote delimited, so there is no need to escape the double-quotes, but the single-quote within the string must be. In Delphi this involves only doubling the single quote sequence and there is no need to escape the '\' or '"' characters at all.

Hopefully this will help you understand any mistakes that you may be making in the representation of your JSON strings.

Upvotes: 2

Related Questions