2

How should I encode this data as JSON in javascript?

I use javascript to get an arbitrary number of 'tags' on photos. Each tag has a firstname and a lastname in this form:

firstname = 'john';
lastname  = 'doe';

firstname = 'jane';
lastname  = 'smith';

I want to encode this data as JSON, send it to my server with Ajax, and decode it in PHP.

My thought was to create a multidimensional array. Is there a better way to do this?

The output of JSON.stringify() is [{\"firstname\":\"John\",\"lastname\":\"Doe\"},{\"firstname\":\"Jane\",\"lastname\":\"Smith\"}]. How can I have JSON.stringify() not escape all of the quotes?

8
  • Unless youre using an API the requires you to send a request in JSON its much easier just send a standard post to the server. Commented Dec 25, 2012 at 3:11
  • I'm using FormData, so unfortunately it has to be sent as JSON Commented Dec 25, 2012 at 3:11
  • Not sure what FormData is... is that a .Net thing? Commented Dec 25, 2012 at 3:12
  • 1
    Curious, you've tagged the question as [json] and [stringify] but apparently JSON.stringify() is the one thing you've not tried. Commented Dec 25, 2012 at 3:13
  • I was trying JSON.stringify(), and it seems to format my data incorrectly, so I figured I'd check here. Commented Dec 25, 2012 at 3:13

3 Answers 3

4

JSON is inspired by JavaScript's object syntax, so all you need to do is create an array of objects:

var data = [
  {
    firstname: 'john',
    lastname: 'doe'
  },
  {
    firstname: 'jane',
    lastname: 'smith'
  }
]

var json = JSON.stringify( data ); // send this object to server
Sign up to request clarification or add additional context in comments.

3 Comments

Note: JSON.stringify/JSON.parse require a shim on IE7 (ick!) and before.
When I do this I end up with [{"firstname":"John","lastname":"Doe"},{"firstname":"Jane","lastname":"Smith"}], and it seems that PHP's json_decode can't get a result out of this
The output of JSON.stringify() is [{\"firstname\":\"John\",\"lastname\":\"Doe\"},{\"firstname\":\"Jane\",\"lastname\":\"Smith\"}]. How can I have JSON.stringify() not escape all of the quotes?
1

JSON is the way to go

JAVASCRIPT

 var data = [{
    firstname: 'john',
    lastname: 'doe'
    },
    {
    firstname: 'jane',
    lastname: 'smith'
   }
 ]

var json = JSON.stringify( data ); 

Now your JSON string will be

[{"firstname":"John","lastname":"Doe"},{"firstname":"Jane","lastname":"Smith"}]

PHP

$arr=json_decode('$jsonVar');

Make sure you put the single quotes cover on the php side. Otherwise PHP won't read this as a string

1 Comment

I'm pretty sure you don't have to wrap it in single quotes. Why would you need to do this? It's supposed to be evaluating the value of the variable, not reading the string.
0

you can convert your tags into json object

var jsonObj = '{"TeamList" : [{"teamid" : "1","teamname" : "Barcelona"}]}';
var obj = $.parseJSON(jsonObj);

or

 var jsonObj = {
"employees": [
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
var obj = $.parseJSON(jsonObj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.