【问题标题】:Problems creating JSON arrays from php JSON object从 php JSON 对象创建 JSON 数组的问题
【发布时间】:2011-08-05 17:01:50
【问题描述】:

我从 php.ini 传回一个 JSON 对象(由字符串数组组成)。我正在尝试将对象转换为 Java jArray,但从我的 php 文件返回的字符串格式不正确:

来自 php 的字符串(我的数据库提示表中有 2 个条目)

[{"0":"2","id":"2","1":"2","household_id":"2","2":"3","stepgreen_id":"3","3":"tip 1","tip":"tip 1","4":"2011-08-05","dateOfTip":"2011-08-05","5":"3","likes":"3"}]

[{"0":"2","id":"2","1":"2","household_id":"2","2":"3","stepgreen_id":"3","3":"tip 1","tip":"tip 1","4":"2011-08-05","dateOfTip":"2011-08-05","5":"3","likes":"3"},{"0":"91","id":"91","1":"1","household_id":"1","2":"1","stepgreen_id":"1","3":"tip 2","tip":"tip 2","4":"2011-08-04","dateOfTip":"2011-08-04","5":"1","likes":"1"}]

这是我的 php 代码

    <?php
    // mysql connection, etc....
    $query  = "SELECT * FROM Tips";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result))
    {
    $output[]=$row;
    print(json_encode($output));
    } 
    mysql_close($con);
    ?>

在 Java 中,我执行以下操作。发生的事情是我得到了一个只有一个条目的 jArray。我期望 2。我不确定为什么 php json 对象返回一个包含 1 个字符串的数组和第二个包含 2 个字符串的数组。我只希望收到一个包含 2 个字符串的数组。

     try {
      InputStream responseData;
      responseData = httpEntity.getContent();
      js = convertStreamToString(responseData);
      Log.v(LOG_TAG, js);
      jArray = new JSONArray(js);

      JSONObject json_data = null;

      Log.v(LOG_TAG, "Arraysize: " + jArray.length());
         for (int i = 0; i < jArray.length(); i++) {
        Log.v(LOG_TAG, "entering loop");
        json_data = jArray.getJSONObject(i);
        tip = json_data.getString("tip");
        stepgreenId = json_data.getString("stepgreen_id");
      dateOfTip = json_data.getString("dateOfTip");
      householdId = json_data.getString("household_id");
      likes = json_data.getString("likes");
      Log.v(LOG_TAG, "Json tip= " + tip + " stepgreen id: " + stepgreenId +  " household_id: " + householdId + " likes: " + likes);

    }
} catch (IllegalStateException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

【问题讨论】:

  • 运行时的js是什么?

标签: java php android json


【解决方案1】:

如果您执行以下操作:

$first_array = array('first array');
$second_array = array('second array');

echo json_encode($first_array);
echo json_encode($second_array);

如果您想反序列化,您将获得的 JSON 是无效的。您必须为它们创建一个公共数组,然后将其打印出来。在你的情况下:

<?php
// mysql connection, etc....
$query  = "SELECT * FROM Tips";
$result = mysql_query($query);
$output = array();
while($row = mysql_fetch_array($result))
    $output[]=$row;

print(json_encode($output));
mysql_close($con);

这应该可行。

【讨论】:

    【解决方案2】:

    不太了解java,但在PHP 方面,您应该尝试改用mysql_fetch_assocmysql_fetch_objectmysql_fetch_array 获取重复的数字和字符串键,数字键可能导致 java 中的问题。

    【讨论】:

      【解决方案3】:

      正确的方法是返回一个包含两个(或更多)对象的 json 字符串。

      $first_array = array(...);
      $second_array = array(...);
      
      die(json_encode(array(
          $first_array,
          $second_array
      )));
      

      我还建议使用 jQuery 的 getJSON() 函数。它只是返回一个您可以轻松读取/操作的 JS 对象。 api.jquery.com/jQuery.getJSON

      $.getJSON('yourFeedPage.php', 'id=xyz123', function(obj) {
          for (int i = 0; i < obj.length(); i++) {
              var item = obj[i];
      
              tip = item.tip;
              stepgreenId = item.stepgreen_id;
              dateOfTip = item.dateOfTip;
              householdId = item.household_id;
              likes = item.likes;
          }
      );
      

      【讨论】:

        【解决方案4】:

        您的print_r() 在您的while() 循环中,因此您正在为查询结果集中的每一行转储一个json 字符串,并且该字符串会随着您添加更多记录而不断增长。这意味着如果你得到 5 行,你将有 5 个 json 字符串转储出来。

        Java 很可能只获取这些 json 字符串中的第一个(第一个/单个记录)并将其余部分丢弃在地板上。

        把你的代码改成这样,事情就应该开始工作了:

        while($row = mysql_fetch_array($result)) {
            $output[] = $row;
        }
        print(json_encode($output));
        

        【讨论】:

        • 是的,这有帮助,还有语句:$output = array();在while循环之前。谢谢!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-03
        • 2017-09-27
        • 2018-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多