【问题标题】:PHP return Json String into JsOnObject failPHP将Json字符串返回到JsOnObject失败
【发布时间】:2014-04-01 22:27:18
【问题描述】:

这是 php 返回的 json 字符串

<?php
date_default_timezone_set('Asia/Kuala_Lumpur');
$data = file_get_contents("php://input");
//echo $data;
//$obj = var_dump(json_decode($data));

$json = json_decode($data);


mysql_connect("localhost", "root", "123456") or die("Could not connect");
mysql_select_db("db_shuttlebus") or die("Could not select database");

if (is_object($json)) {
    $d = array();
    foreach($json->details as $obj) {
        $Ticket_No = $obj->{'Ticket_No'};
        $Ticket_Date = $obj->{'Ticket_Date'};
        $Amount = $obj->{'Amount'};
        $In_Out = $obj->{'In_Out'};
        $Vehicle_ID = $obj->{'Vehicle_ID'};     
        $From_LocationID = $obj->{'From_LocationID'};
        $PriceType_ID = $obj->{'PriceType_ID'};
        $Trip_ID = $obj->{'Trip_ID'};
        $To_LocationID = $obj->{'To_LocationID'};
        $Inspector_Print = $obj->{'Inspector_Print'};
        $Driver_ID = $obj->{'Driver_ID'};
        $Updated_Time = date("Y-m-d H:i:s");
        $Route_ID = $obj->{'Route_ID'};
        //echo $_id;
        $query = "INSERT INTO tbl_ticket (Ticket_No,Ticket_Date,Amount,In_Out,Vehicle_ID,From_LocationID,PriceType_ID,Trip_ID,To_LocationID,Inspector_Print,Driver_ID,Updated_Time,Route_ID)VALUES('".$Ticket_No."','".$Ticket_Date."','".$Amount."','".$In_Out."','".$Vehicle_ID."','".$From_LocationID."','".$PriceType_ID."','".$Trip_ID."','".$To_LocationID."','".$Inspector_Print."','".$Driver_ID."','".$Updated_Time."','".$Route_ID."')";

        $rs = mysql_query($query) or die ("Error in query: $query " . mysql_error());
            if ($rs) { 
                $d[] = array('Ticket_No' => $Ticket_No ,'Updated_Time' => $Updated_Time);
            }
            //break;
    }
    $pass_json = json_encode($d);
    echo $pass_json;

}
?>

这是我的 json 字符串

[{"Ticket_No":1950,"Updated_Time":"2014-03-01 02:15:02"},
{"Ticket_No":1951,"Updated_Time":"2014-03-01 02:15:02"},
{"Ticket_No":1952,"Updated_Time":"2014-03-01 02:15:02"},
{"Ticket_No":1953,"Updated_Time":"2014-03-01 02:15:02"}]

我试图将我的 json 字符串放入 JsonObject 中,并放入一个映射,然后使用映射来获取每一行 Ticket_No 值,但不幸的是失败了,如何让我将 json 字符串放入 Jsonobject 或 jsonarray 并循环行获取Ticket_NoUpdated_Time

     try {
           JSONObject jsonObject = new JSONObject(jsonstring);
           Iterator keys = jsonObject.keys();
           Map<String, String> map = new HashMap<String, String>();
              while (keys.hasNext()) {
                 String key = (String) keys.next();
                 map.put(key, jsonObject.getString(key));
              }
           System.out.println(map);
          } catch (JSONException e) {
            e.printStackTrace();
          }

JSONArray jsonArray = new JSONArray(jsonstring);
                       JSONArray jsonPersonData = jsonArray.getJSONArray(1);
                       for (int i=0; i<jsonPersonData.length(); i++) {
                           JSONObject item = jsonPersonData.getJSONObject(i);
                           String Ticket_No = item.getString("Ticket_No");
                           Log.d(null,"Ticket_No= "+Ticket_No);
                           String Updated_Time = item.getString("Updated_Time");
                       }

也不能,为什么?

【问题讨论】:

    标签: java php json


    【解决方案1】:

    因为不是完整的JSON,试试做

    {"tickets": [ 
       {"Ticket_No":1950,"Updated_Time":"2014-03-01 02:15:02"},
       {"Ticket_No":1951,"Updated_Time":"2014-03-01 02:15:02"},
       {"Ticket_No":1952,"Updated_Time":"2014-03-01 02:15:02"},
       {"Ticket_No":1953,"Updated_Time":"2014-03-01 02:15:02"}
    ]}
    

    我建议在 for 循环结束时做这样的事情

    $return_json = array('tickets' => array($d));
    $pass_json = json_encode($return_json);
    echo $pass_json;
    

    【讨论】:

    • 不,我认为这是正确的 json 字符串,因为从 php 数组转换为 json my1.php.net/json_encode
    • 你将不得不看看 JSONObject 期望它看起来如何
    • 您使用的是哪个 JSON 库?
    • $d = array(); 这是$d
    • 对于你的java类,你使用的是哪个json库?
    猜你喜欢
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    相关资源
    最近更新 更多