如何迭代多个JSON响应?

时间:2022-09-10 13:38:08

I want to return multiple responses from PHP to my Android device. In PHP I have something like:

我想从PHP返回多个响应到我的Android设备。在PHP中我有类似的东西:

while ($row = mysql_fetch_array($data)) {
    $response["post"] = strip_tags($row["post_text"]);
    $response["date"] = date('D M d, Y', $row["post_time"]);
    echo json_encode($response);
}

In Logcat I get:

在Logcat我得到:

{"tag":"midnightAnnouncements","success":1,"error":0,"post":"This is a test announcement! ","date":"Sun Oct 21, 2012"}{"tag":"midnightAnnouncements","success":1,"error":0,"post":"Here is another ","date":"Sun Oct 21, 2012"}n

{“tag”:“midnightAnnouncements”,“success”:1,“error”:0,“post”:“这是测试公告!”,“日期”:“Sun Oct 21,2012”} {“tag” :“midnightAnnouncements”,“成功”:1,“错误”:0,“发布”:“这是另一个”,“日期”:“Sun Oct 21,2012”} n

These are the results of two rows. I'd like to iterate over the JSON object and get the strings for both "post"s. How can this be done in Java?

这些是两行的结果。我想迭代JSON对象并获取两个“post”的字符串。如何在Java中完成?

2 个解决方案

#1


0  

You can wrap the output in a JSON array:

您可以将输出包装在JSON数组中:

$output = array();

while ($row = mysql_fetch_array($data)) {
    $response["post"] = strip_tags($row["post_text"]);
    $response["date"] = date('D M d, Y', $row["post_time"]);

    $output[] = $response;
}
echo json_encode($output);

#2


0  

Try this -

试试这个 -

HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONArray array = new JSONArray(json);
String post;
for (int i = 0; i < array.length(); i++) {
    JSONObject row = array.getJSONObject(i);
    post = row.getString('post');
}

And as nneonneo pointed out, your response from server is not in correct format. You should first wrap everything in a two dimensional array and then echo json_encoded version.

正如nneonneo指出的那样,您对服务器的响应格式不正确。您应首先将所有内容包装在二维数组中,然后回显json_encoded版本。

$i=0;
while ($row = mysql_fetch_array($data)) {
    $response[$i]["post"] = strip_tags($row["post_text"]);
    $response[$i]["date"] = date('D M d, Y', $row["post_time"]);
    $i++;
}
echo json_encode($response);

#1


0  

You can wrap the output in a JSON array:

您可以将输出包装在JSON数组中:

$output = array();

while ($row = mysql_fetch_array($data)) {
    $response["post"] = strip_tags($row["post_text"]);
    $response["date"] = date('D M d, Y', $row["post_time"]);

    $output[] = $response;
}
echo json_encode($output);

#2


0  

Try this -

试试这个 -

HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONArray array = new JSONArray(json);
String post;
for (int i = 0; i < array.length(); i++) {
    JSONObject row = array.getJSONObject(i);
    post = row.getString('post');
}

And as nneonneo pointed out, your response from server is not in correct format. You should first wrap everything in a two dimensional array and then echo json_encoded version.

正如nneonneo指出的那样,您对服务器的响应格式不正确。您应首先将所有内容包装在二维数组中,然后回显json_encoded版本。

$i=0;
while ($row = mysql_fetch_array($data)) {
    $response[$i]["post"] = strip_tags($row["post_text"]);
    $response[$i]["date"] = date('D M d, Y', $row["post_time"]);
    $i++;
}
echo json_encode($response);