jquery, ajax, json解决方案
jquery, ajax, json
需要解析的 数据格式
{
"status":0,
"message":"ok",
"total":4030,
"results":[
{
"name":"成大方圆连锁药店青堆分店",
"location":{
"lat":39.836746,
"lng":123.271671
},
"address":"双河大街598号附近",
"detail":1,
"uid":"7388dffd069db9ebd3ff2363",
"detail_info":{
"tag":"医疗;药店",
"type":"hospital",
"detail_url":"http://api.map.baidu.com/place/detail?uid=7388dffd069db9ebd3ff2363&output=html&source=placeapi_v2",
"price":"0",
"overall_rating":"2.9",
"service_rating":"0",
"technology_rating":"0",
"image_num":"1"
}
},
{
"name":"安康大药房昌盛店",
"location":{
"lat":39.699406,
"lng":122.983766
},
"address":"延安路芙蓉小区附近",
"telephone":"(0411)89838258",
"detail":0,
"uid":"2a7a25ec5f11c91d6c3e1bf2",
"detail_info":{
"tag":"医疗;药店"
}
},
想要得到name,address,telephone,detail_url等信息。
前台:
<script type="text/javascript">
function GetDrugStoreData() {
$.ajax({
type: "post",
url: "http://api.map.baidu.com/place/v2/search?ak=BHYd9MhP6fMWrASjxtEYOGtz&output=json&query=%E8%8D%AF%E6%88%BF&page_size=10&page_num=0&scope=2®ion=%E5%A4%A7%E8%BF%9E",
dataType: "json",
success: function (data) {
$("input#showTime").val(data[0].demoData);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
</script>
后台:
Dim jsonstr As String = ""
jsonstr = JsonConvert.SerializeObject(DrugInfo)
If Request.QueryString("ajax") IsNot Nothing Then
Dim method As String = ""
method = Request.QueryString("ajax").ToString().Trim().ToLower()
If method = "GetDrugStoreData" Then
Response.Clear()
Response.Write(jsonstr)
Response.End()
End If
End If
完全不懂这方面,请大家帮忙。
------解决思路----------------------
你是前端在js里解析json还是VB.NET后端解析?
前端的话,如果你那个是字符串,那么用 eval('('+data+')')就可以转成json数组
后端的用,看你也用了JsonConvert,用这个反序列化就行了
------解决思路----------------------
当你将 ajax 标记为 dataType: "json" 属性时,你得到的返回(回调参数)就是 javascript对象。因此直接访问其属性就行了。
javascript是弱类型的,所以在vs上编程时没有直观的属性提示帮助,也容易在运行时才崩溃。这些代价换来的主要就是书写简单。
------解决思路----------------------
你要如何操作?从你贴出哪条代码上体现出需要操作来?
------解决思路----------------------
后台在输出的时候,content-type设置 一下
前台感觉可以直接用alert(data.message);
------解决思路----------------------
前台可以将你的json字符串转为对象。。
需要解析的 数据格式
{
"status":0,
"message":"ok",
"total":4030,
"results":[
{
"name":"成大方圆连锁药店青堆分店",
"location":{
"lat":39.836746,
"lng":123.271671
},
"address":"双河大街598号附近",
"detail":1,
"uid":"7388dffd069db9ebd3ff2363",
"detail_info":{
"tag":"医疗;药店",
"type":"hospital",
"detail_url":"http://api.map.baidu.com/place/detail?uid=7388dffd069db9ebd3ff2363&output=html&source=placeapi_v2",
"price":"0",
"overall_rating":"2.9",
"service_rating":"0",
"technology_rating":"0",
"image_num":"1"
}
},
{
"name":"安康大药房昌盛店",
"location":{
"lat":39.699406,
"lng":122.983766
},
"address":"延安路芙蓉小区附近",
"telephone":"(0411)89838258",
"detail":0,
"uid":"2a7a25ec5f11c91d6c3e1bf2",
"detail_info":{
"tag":"医疗;药店"
}
},
想要得到name,address,telephone,detail_url等信息。
前台:
<script type="text/javascript">
function GetDrugStoreData() {
$.ajax({
type: "post",
url: "http://api.map.baidu.com/place/v2/search?ak=BHYd9MhP6fMWrASjxtEYOGtz&output=json&query=%E8%8D%AF%E6%88%BF&page_size=10&page_num=0&scope=2®ion=%E5%A4%A7%E8%BF%9E",
dataType: "json",
success: function (data) {
$("input#showTime").val(data[0].demoData);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
</script>
后台:
Dim jsonstr As String = ""
jsonstr = JsonConvert.SerializeObject(DrugInfo)
If Request.QueryString("ajax") IsNot Nothing Then
Dim method As String = ""
method = Request.QueryString("ajax").ToString().Trim().ToLower()
If method = "GetDrugStoreData" Then
Response.Clear()
Response.Write(jsonstr)
Response.End()
End If
End If
完全不懂这方面,请大家帮忙。
------解决思路----------------------
你是前端在js里解析json还是VB.NET后端解析?
前端的话,如果你那个是字符串,那么用 eval('('+data+')')就可以转成json数组
后端的用,看你也用了JsonConvert,用这个反序列化就行了
------解决思路----------------------
当你将 ajax 标记为 dataType: "json" 属性时,你得到的返回(回调参数)就是 javascript对象。因此直接访问其属性就行了。
javascript是弱类型的,所以在vs上编程时没有直观的属性提示帮助,也容易在运行时才崩溃。这些代价换来的主要就是书写简单。
------解决思路----------------------
你要如何操作?从你贴出哪条代码上体现出需要操作来?
------解决思路----------------------
后台在输出的时候,content-type设置 一下
前台感觉可以直接用alert(data.message);
------解决思路----------------------
前台可以将你的json字符串转为对象。。