0

我有一个从数据库收集评论的 json 函数。所有的评论都收集在一个 php 数组中并发送回手机并变成一个 jsonobject。这是我的json代码

    JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);

return json2;

当它返回它时,json看起来像这样

 {
"tag": "collectComments",
"success": 1,
"error": 0,
"numberOfComments": 16,
"offsetNumber": 1,
"comment": [
    "test 16",
    "test 15",
    "test 14",
    "test 13",
    "test 12",
    "test 11",
    "test 10",
    "test 9",
    "test 8",
    "test 7",
    "test 6",
    "test 5",
    "test 4",
    "test 3",
    "test 2",
    "test 1"
]
}   

如何将所有评论分开并将它们放在自己的文本视图中?像这样在Activity开头将comment变量归类为字符串,

    private static String KEY_COMMENT = "comment";

这应该设置为数组吗?如果是这样,那么我怎样才能将每个评论放入他们自己的 textView 中?

4

1 回答 1

3

使用需要使用getJSONArray("comment"); 物体上。

然后,您可以遍历每个数组项。

JSONObject test = new JSONObject("...");
JSONArray array = test.getJSONArray(KEY_COMMENT);
for(int i = 0; i < array.length(); i++) {
    textView[i].setText(array.getString(i));
}
于 2013-07-06T16:35:23.963 回答