Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi I'm trying to insert the json array into my MySQL database.With array json data from android client.

[{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"[email protected]"}]
share|improve this question

4 Answers 4

If you want to store the array as a string, you can use JSON.stringify():

$string = [{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"[email protected]"}];    
$json = JSON.stringify($string);

The variable $json is then a simple string which can be inserted into MySQL easily.

You can then use:

var obj = JSON.parse($json);

To convert the string back to an array.

This method usually isn't recommended for performance reasons though, so you might alternatively want to break up the array and store each field individually.

share|improve this answer

Try this:

$json = serialize(json_array);

share|improve this answer

Use $jsonArray = json_decode($jsonStr);. Then iterate the array as you want to save data in your mysql database.

share|improve this answer

you can use - serialize()

$json = '[{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"[email protected]"}]';
$newJson = serialize(json_decode($json));

$newJson is ready to be inserted. and after fetching -

$data = unserialize($fetchedData); and then json_encode($data);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.