-2

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]"}]

4 Answers 4

2

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.

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

Comments

1

Try this:

 $json = serialize(json_array);

Comments

0

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

Comments

0

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);

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.