【发布时间】:2011-04-18 02:51:04
【问题描述】:
我有一个设置,它可以返回相当数量的信息。这是logcat:
04-17 22:38:21.886: DEBUG/TestMYSQL(12603): Result of sql:
[{"id":"1","front_text":"the dog was so cute","back_text":"its name was dolly"},
{"id":"2","front_text":"plants use the sun","back_text":"isn't that interesting"},
{"id":"3","front_text":"plants can use the sun to create","back_text":"energy"},
{"id":"4","front_text":"a plant also needs minerals","back_text":"from the soil"},
{"id":"5","front_text":"without water the plant would","back_text":"probably die"},
{"id":"6","front_text":"plants are little machines","back_text":"who love to eat"}]
为了获得这些信息,我让它执行一个 php 文件。下面是一些相关的Java、android代码:
.....
result = sb.toString();
Log.d(TAG, "Result of sql: " + result);
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
JSONObject json_data = null;
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
}
return json_data;
它将结果转换成一个 jsonObject,我可以使用它。
所以这里是之前的一步,php中的查询部分:
include 'connectMySQL.php';
mysql_select_db("card_db");
$q=mysql_query("SELECT * FROM ".$packname);
while($e=mysql_fetch_assoc($q)){
$output[]=$e;
}
print(json_encode($output));
mysql_close();
问题是我似乎只能访问 FINAL... eh 行。也就是说,在上面的数据中,我似乎只能访问到 id:6 行。
我正在查看 auto_complete 和 JSONOBJECT,但目前我没有足够的经验来解决这个问题,而且已经很晚了。
关于如何在 java 中循环 jsonObject 有什么想法吗?
让我花一点时间分析一下结构,然后再上交。我对 JSON 了解不多,但它是这样的:
我用我设置的表查询数据库,等等。 它返回数据行。 我想我的问题是如何将一行键值对编码为 JSON OBJECT,以及如何访问不同的“行”?
【问题讨论】: