this is 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;
}
?>
this is my 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"}]
i m trying to put my json string in JsonObject, and put in a map, then use a map to get the every row Ticket_No
value, but unfortunately failed, how to let me json string into Jsonobject or jsonarray and loop the row get the Ticket_No
and 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();
}
or
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");
}
also can't, why?