我试图在android中解析此json数据:
<script type="text/javascript">
$(document).ready(function(){
var category = document.getElementById("category").value;
$.ajax({
type:'POST',
url:'<?=base_url();?>dashboard/ajaxData',
data:'main_category='+category,
success:function(html){
$('#services').html(html);
}
});
$('#category').on('change',function(){
var category = $(this).val();
if(category){
$.ajax({
type:'POST',
url:'<?=base_url();?>dashboard/ajaxData',
data:'main_category='+category,
success:function(html){
$('#services').html(html);
}
});
}else{
$('#services').html('<option value="">Select category first</option>');
}
});
});
var x = $("#services").find(':selected').attr('data-price');
document.getElementById("demo").innerHTML = x;
</script>
<div class="form-group">
<label>Category</label>
<select name="category" id="category" class="form-control">
<?php foreach($categories->result() as $cat): ?>
<option data-price="55" value="<?=$cat->id;?>">- <?=$cat->title;?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label>Service</label>
<select name="service" id="services" class="form-control">
<option data-price=""></option>
</select>
</div>
<div id="demo"></div>
这是android volley代码
{
"status": true,
"data": [{
"id": 1,
"name": "test",
"notes": "notes",
"types": [{
"id": 2,
"name": "cases",
"created_at": "2018-06-09 07:36:52",
"updated_at": "2018-06-09 07:36:52",
"pivot": {
"trip_id": 1,
"type_id": 2
}
}, {
"id": 3,
"name": "bags",
"created_at": "2018-06-09 07:42:13",
"updated_at": "2018-06-09 07:42:13",
"pivot": {
"trip_id": 1,
"type_id": 3
}
}
],
"user": {
"id": 1,
"name": "admin",
"email": "admin@gmail.com",
"group_id": 1,
"api_token": "toOarWXv2VezUxoXaoGbKdvtr3pBYmkv68cI7gxDMxgKoYjnKeiYSzrGqOr2",
"created_at": "2018-04-26 20:16:28",
"updated_at": "2018-04-26 20:16:28"
},
"date": "2018/06/20",
"time": "11:50 AM",
"fromcountry_id": 1,
"tocountry_id": 1,
"fromcity_id": 1,
"tocity_id": 1,
"estimated_time": "hjk"
}
],
"error": ""
}
响应变量真正返回值,但是用json进行的解析不起作用..怎么了?
答案 0 :(得分:0)
您的JSON
解析错误
您的new JSONObject().get("Data")
为空
像下面的代码一样更改您的JSON
解析
使用此
JSONObject jsonObject= new JSONObject(response);
JSONArray jsonArray =jsonObject.getJSONArray("data");
代替此
JSONArray jsonArray = new JSONArray(new JSONObject().get("Data"));