0
嘗試讀取JSON數組對象時遇到問題。如何讀取Windows Phone中的JSON數組值?
我正在使用PHP來創建JSON數據。
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["records"] = array();
//$response["products"] = array();
while ($row = mysql_fetch_array($result))
{
// temp user array
$record = array();
$record["vehicleNo"] = $row["vehicleNo"];
$record["vehicleType"] = $row["vehicleType"];
$record["inTime"] = $row["inTime"];
$record["outTime"] = $row["outTime"];
$record["entryUser"] = $row["entryUser"];
// push single record into final response array
array_push($response["records"], $record);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
上面的代碼返回以下JSON:
{"records":[{"vehicleNo":"6545","vehicleType":"Bike","inTime":"2013-08-01 18:11:20","outTime":"2013-08-01 19:27:55","entryUser":"srini"}],"success":1}
我使用下面的代碼來解析JSON:
var rootObject = JsonConvert.DeserializeObject<MyBookList>(e.Result);
Console.WriteLine(rootObject.success);
MessageBox.Show(rootObject.success.ToString());
foreach (var re in rootObject.recordsss)//
{
Debug.WriteLine(re.value_record);
MessageBox.Show(re.value_record);
}
public class MyBookList
{
public string success { get; set; }
public MyRecords[] recordsss { get; set; }
}
public class MyRecords
{
public string value_record { get; set; }
}
但將不設置「對象引用到對象的實例「錯誤。
如何解決此問題並正確讀取JSON數據。
你的代碼工作羅傑斯.... – selvam