2

I want to send string array to php by Json and retrieve there.

String[] Books = {"book1", "book2", "book3", "book4",..};

In Json:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>;
nameValuePairs.add(new BasicNameValuePair("books", Books));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
...

In php side:

$books = $_POST['books'];
$result = json_decode($books);    // is this OK?

Or Can I use it like below?

$book1 = $result[0];
2
  • if u send data as Json then u need json_decode() but for row data without any json format the post data will be in array form. Commented Apr 6, 2014 at 15:32
  • I'm sending by json. The thing is that I'm not sure if this method is true or not. Is $result and array or not. Is nameValuePairs.add(new BasicNameValuePair("books", Books)); a good method for sending string array or should I use nameValuePairs.add(new BasicNameValuePair("books", Books[])); here? Commented Apr 6, 2014 at 15:59

1 Answer 1

1
$jsonString = '["[email protected]","[email protected]","[email protected]"]';
$arrayOfYourEmails=json_decode($jsonString);

Or

$jsonString = "[\"[email protected]\",\"[email protected]\",\"[email protected]\"]";
$arrayOfYourEmails=json_decode($jsonString);

and yes your this code is right

$books = $_POST['books'];
$result = json_decode($books); 
Sign up to request clarification or add additional context in comments.

1 Comment

In $arrayOfYourEmails=json_decode($jsonString); you are decoding a static $jsonString. But I need to decode String[] Books = ["book1", "book2", "book3", "book4",...].

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.