duancai7002 2015-10-29 17:34
浏览 55

如何通过JSON将PHP的输出转换为解析数据

I have the following php file working fine on a webhost.

<?php
   $dbhost = 'dbhost';
   $dbuser = 'dbuser';
   $dbpass = 'pass';

   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn )
   {
      die('Could not connect'.mysql_error());
   }

   $sql = "SELECT FIELD1, FIELD2, FIELD3, FIELD4 FROM `TABLE` WHERE FIELD1 =     'VALUE1' ";
   mysql_select_db('MyDB');
   $retval = mysql_query( $sql, $conn );

   if(! $retval )
   {
      die('Could not get data: ' . mysql_error());
   }

   while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
   {
      echo "FIELD1" :{$row['FIELD1']}  <br> ".
         "FIELD2 : {$row['FIELD2']} <br> ".
         "FIELD3 : {$row['FIELD3']} <br> ";
      $output[] = $row;
   }

   print(json_encode($row));
   mysql_close($conn);
?>

Link is here http://marcodr.byethost7.com/TP2.php

Now, I would like to show this content in a TextView and I am unseccessfully using the following code

JSONObject object=getJSONObject(“result”); //comes from the     print(json_encode ($result));
JSONArray arr=object.getJSONArray(“Value1”); //transforms the object Value1 in array
for(int i=0;i<arr.length;i++){
JSONObject obj1=arr.getJSONObject(i);
String Value1=obj1.getString("Value1");
TextView.setText(Value1); //set TextView’s content to the String Value1
}

The manifest has permission to navigate the internet, the text view is in the layout etc, but STILL I can't put the PHP output in the TextView. What is wrong in my code?

  • 写回答

2条回答 默认 最新

  • dpgbh20688 2015-10-29 18:54
    关注

    In PHP side:

    $obj = new stdClass();
    $obj->label="Price of an item";
    $obj->data = array(
        array('1999','200'),
        array('2000','210'),
        array('2007','240')
    );
    
    echo json_encode($obj);
    

    Let's suppose your JSON is this:

    [
        {
            "response": [
                {
                    "1999": "200",
                    "2000": "210",
                    "2007": "240"
                }
            ]
        }
    ]
    

    In Android side:

    String jsonString3 =
                    "[
    " +
                            "    {
    " +
                            "        \"response\": [
    " +
                            "            {
    " +
                            "                \"1999\": \"200\",
    " +
                            "                \"2000\": \"210\",
    " +
                            "                \"2007\": \"240\"
    " +
                            "            }
    " +
                            "        ]
    " +
                            "    }
    " +
                            "]";
            try{
                JSONArray array = new JSONArray(jsonString3);
                JSONArray priceArray = array.getJSONObject(0).getJSONArray("response");
                Log.d(LOG_TAG,"Price Array = " + priceArray.toString());
    
                for (int i = 0; i < priceArray.length(); i++){
                    Log.d(LOG_TAG, "1999 = " + priceArray.getJSONObject(i).getString("1999"));
                    Log.d(LOG_TAG, "2000 = " + priceArray.getJSONObject(i).getString("2000"));
                    Log.d(LOG_TAG, "2007 = " + priceArray.getJSONObject(i).getString("2007"));
                }
    
            }catch (JSONException ex){
                Log.e(LOG_TAG, ex.toString());
            }
    

    Suppose you received jsonData from your PHP api. I hope this helps you:

    boolean success = false;
            int temp= 0;
            try {
                JSONObject jsonobject = new JSONObject(jsonData);
                success = jsonobject.getBoolean("success");
                if (success) {
                    // Locate the NodeList name
                    JSONArray jsonarray = jsonobject.getJSONArray("data");
                    for (int i = 0; i < jsonarray.length(); i++) {
                        temp = i+1;
                        jsonobject = jsonarray.getJSONObject(i);
                        String id = jsonobject.getString("FIELD"+temp);
                        String values = jsonobject.getString("value");
                        Log.e(LOG_TAG, "FIElD"+temp+": "+id);
                        Log.e(LOG_TAG, "Value: "+values);
                    }
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器