这是php return json string
<?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 String
[{"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_No
和Updated_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");
}
也不能,为什么?
答案 0 :(得分:0)
因为它不是完整的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;