将字符串数组发送到php

时间:2014-04-06 15:30:00

标签: php android arrays json android-json

我想通过Json将字符串数组发送到php并在那里检索。

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

在Json:

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

在php方面:

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

或者我可以像下面一样使用它吗?

$book1 = $result[0];

1 个答案:

答案 0 :(得分:1)

$jsonString = '["xxx@gmail.com","yy@gmail.com","rr@gmail.com"]';
$arrayOfYourEmails=json_decode($jsonString);

或者

$jsonString = "[\"xxx@gmail.com\",\"yy@gmail.com\",\"rr@gmail.com\"]";
$arrayOfYourEmails=json_decode($jsonString);

是的,你的代码是对的

$books = $_POST['books'];
$result = json_decode($books);