【问题标题】:PHP/Android and JSONObject; I can only seem to access the last elementPHP/Android 和 JSONObject;我似乎只能访问最后一个元素
【发布时间】: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,以及如何访问不同的“行”?

【问题讨论】:

    标签: php android mysql json


    【解决方案1】:

    您正在覆盖循环的每次迭代中 json_data 引用的 JSONObject。所以最后总是返回jArray中的最后一个元素。

    由于您需要返回多个对象,您可以:

    • 只需将jArray 返回给调用函数。但是,这意味着调用者必须处理数据传输实现的细节,如果您决定更改库或迁移到 XML,它将破坏大量代码并使其变得更加困难。

    • 返回调用代码知道并应该处理的实际对象的数组或列表。例如,您可以声明一个具有 id, front_text, back_text 的值对象 (VO),并且对于 jArray 中的每个 JSONObject,您将创建一个新的 VO 并将其放入一个数组并返回。

      public class MyVO
      {
          final public String id;
          final public String frontText;
          final public String backText;
      
          public MyVO(String id, String ft, String bt)
          {
              this.id = id;
              this.frontText = ft;
              this.backText = bt;
          }
      }
      
      List<MyVo> vos = new ArrayList<MyVO>();
      JSONArray jArray = new JSONArray(result);
      for (int i = 0; i < jArray.length(); i++) {
         json_data = jArray.getJSONObject(i);
         vos.add(new MyVO(json_data.getString("id"), json_data.getString("front_text"), json_data.getString("back_text"));
      }
      

      在调用代码中,您可以查看 VO:

      for(MyVO vo : vos)
      {
          //vo.id or vo.frontText or vo.backText
      }
      

    【讨论】:

    • 啊啊啊啊我现在看到了。很棒的答案,谢谢。你是一个绅士和一个学者。或者女士。
    猜你喜欢
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2015-04-20
    相关资源
    最近更新 更多