求一个解析以下内容的JS代码
发布于 10 年前 作者 FuGardenia 5280 次浏览 最后一次编辑是 8 年前

http://api.dangqian.com/apidiqu2/api.asp?format=json&callback=wjr&id=000000000000

返回数据

wjr({ “is_login”:1, “site”:“中国行政区划api”, “url”:“http://api.dangqian.com/apidiqu2/”, “jieshao”:“省市县乡村五级行政区划地址信息库”, “idengji”:“1”, “idiming”:“中国”, “idaima”:“000000000000”, “diming1”:“中国”, “daima1”:“000000000000”, “diming2”:"", “daima2”:"", “diming3”:"", “daima3”:"", “diming4”:"", “daima4”:"", “diming5”:"", “daima5”:"", “diming6”:"", “daima6”:"", “zidi”:34, “banben”:“2011”, “zhizao”:“王建然”, “qq”:“250247045”, “riqi”:“2012-12-23 22:06:49”, “shike”:“2013-1-3 22:31:24”, “id”:“35”, “list”:{ “wjr1”:{“dengji”:“2”,“diming”:“北京市”,“daima”:“110000000000”,“zidi”:2}, “wjr2”:{“dengji”:“2”,“diming”:“天津市”,“daima”:“120000000000”,“zidi”:2}, “wjr3”:{“dengji”:“2”,“diming”:“河北省”,“daima”:“130000000000”,“zidi”:11}, “wjr4”:{“dengji”:“2”,“diming”:“山西省”,“daima”:“140000000000”,“zidi”:11}, “wjr5”:{“dengji”:“2”,“diming”:“内蒙古自治区”,“daima”:“150000000000”,“zidi”:12}, “wjr6”:{“dengji”:“2”,“diming”:“辽宁省”,“daima”:“210000000000”,“zidi”:14}, “wjr7”:{“dengji”:“2”,“diming”:“吉林省”,“daima”:“220000000000”,“zidi”:9}, “wjr8”:{“dengji”:“2”,“diming”:“黑龙江省”,“daima”:“230000000000”,“zidi”:13}, “wjr9”:{“dengji”:“2”,“diming”:“上海市”,“daima”:“310000000000”,“zidi”:2}, “wjr10”:{“dengji”:“2”,“diming”:“江苏省”,“daima”:“320000000000”,“zidi”:13}, “wjr11”:{“dengji”:“2”,“diming”:“浙江省”,“daima”:“330000000000”,“zidi”:11}, “wjr12”:{“dengji”:“2”,“diming”:“安徽省”,“daima”:“340000000000”,“zidi”:16}, “wjr13”:{“dengji”:“2”,“diming”:“福建省”,“daima”:“350000000000”,“zidi”:9}, “wjr14”:{“dengji”:“2”,“diming”:“江西省”,“daima”:“360000000000”,“zidi”:11}, “wjr15”:{“dengji”:“2”,“diming”:“山东省”,“daima”:“370000000000”,“zidi”:17}, “wjr16”:{“dengji”:“2”,“diming”:“河南省”,“daima”:“410000000000”,“zidi”:18}, “wjr17”:{“dengji”:“2”,“diming”:“湖北省”,“daima”:“420000000000”,“zidi”:14}, “wjr18”:{“dengji”:“2”,“diming”:“湖南省”,“daima”:“430000000000”,“zidi”:14}, “wjr19”:{“dengji”:“2”,“diming”:“广东省”,“daima”:“440000000000”,“zidi”:21}, “wjr20”:{“dengji”:“2”,“diming”:“广西壮族自治区”,“daima”:“450000000000”,“zidi”:14}, “wjr21”:{“dengji”:“2”,“diming”:“海南省”,“daima”:“460000000000”,“zidi”:3}, “wjr22”:{“dengji”:“2”,“diming”:“重庆市”,“daima”:“500000000000”,“zidi”:2}, “wjr23”:{“dengji”:“2”,“diming”:“四川省”,“daima”:“510000000000”,“zidi”:21}, “wjr24”:{“dengji”:“2”,“diming”:“贵州省”,“daima”:“520000000000”,“zidi”:9}, “wjr25”:{“dengji”:“2”,“diming”:“云南省”,“daima”:“530000000000”,“zidi”:16}, “wjr26”:{“dengji”:“2”,“diming”:“西藏自治区”,“daima”:“540000000000”,“zidi”:7}, “wjr27”:{“dengji”:“2”,“diming”:“陕西省”,“daima”:“610000000000”,“zidi”:10}, “wjr28”:{“dengji”:“2”,“diming”:“甘肃省”,“daima”:“620000000000”,“zidi”:14}, “wjr29”:{“dengji”:“2”,“diming”:“青海省”,“daima”:“630000000000”,“zidi”:8}, “wjr30”:{“dengji”:“2”,“diming”:“宁夏回族自治区”,“daima”:“640000000000”,“zidi”:5}, “wjr31”:{“dengji”:“2”,“diming”:“新疆维吾尔自治区”,“daima”:“650000000000”,“zidi”:15}, “wjr32”:{“dengji”:“2”,“diming”:“台湾省”,“daima”:“710000000000”,“zidi”:0}, “wjr33”:{“dengji”:“2”,“diming”:“香港特别行政区”,“daima”:“810000000000”,“zidi”:0}, “wjr34”:{“dengji”:“2”,“diming”:“澳门特别行政区”,“daima”:“820000000000”,“zidi”:0} } }) 含义:

wjr({ “is_login”:1, “site”:“中国行政区划api”, “url”:“http://api.dangqian.com/apidiqu2/”, “jieshao”:“省市县乡村五级行政区划地址信息库”, “idengji”:"$$等级$$", “idiming”:"$$当前地区$$", “idaima”:"$$当前代码$$", “diming1”:"$$一级地区$$", “daima1”:"$$一级代码$$", “diming2”:"$$二级地区$$", “daima2”:"$$二级代码$$", “diming3”:"$$三级地区$$", “daima3”:"$$三级代码$$", “diming4”:"$$四级地区$$", “daima4”:"$$四级代码$$", “diming5”:"$$五级地区$$", “daima5”:"$$五级代码$$", “diming6”:"$$六级地区$$", “daima6”:"$$六级代码$$", “zidi”:$$子级数目$$, “banben”:"$$版本$$", “zhizao”:“王建然”, “qq”:“250247045”, “riqi”:"$$时间$$", “shike”:"$$服务器时刻$$", “id”:"$$id$$", “list”:{ $$地区列表集合$$ } })

11 回复

截取前面的wjr( 和后面的),然后用JSON.parse转化为json对象

function wjr(data){
 //data就是一个json对象
}
eval(返回值)
str = """
wjr({ "is_login":1, "site":"中国行政区划api", "url":"http://api.dangqian.com/apidiqu2/", "jieshao":"省市县乡村五级行政区划地址信息库", "idengji":"1", "idiming":"中国", "idaima":"000000000000", "diming1":"中国", "daima1":"000000000000", "diming2":"", "daima2":"", "diming3":"", "daima3":"", "diming4":"", "daima4":"", "diming5":"", "daima5":"", "diming6":"", "daima6":"", "zidi":34, "banben":"2012", "zhizao":"王建然", "qq":"250247045", "riqi":"2012-12-23 22:06:49", "shike":"2014-7-14 2:55:33", "id":"380", "list":{ "wjr1":{"dengji":"2","diming":"北京","daima":"110000000000","zidi":2}, "wjr2":{"dengji":"2","diming":"天津","daima":"120000000000","zidi":2}, "wjr3":{"dengji":"2","diming":"河北","daima":"130000000000","zidi":11}, "wjr4":{"dengji":"2","diming":"山西","daima":"140000000000","zidi":11}, "wjr5":{"dengji":"2","diming":"内蒙古","daima":"150000000000","zidi":12}, "wjr6":{"dengji":"2","diming":"辽宁","daima":"210000000000","zidi":14}, "wjr7":{"dengji":"2","diming":"吉林","daima":"220000000000","zidi":9}, "wjr8":{"dengji":"2","diming":"黑龙江","daima":"230000000000","zidi":13}, "wjr9":{"dengji":"2","diming":"上海","daima":"310000000000","zidi":2}, "wjr10":{"dengji":"2","diming":"江苏","daima":"320000000000","zidi":13}, "wjr11":{"dengji":"2","diming":"浙江","daima":"330000000000","zidi":11}, "wjr12":{"dengji":"2","diming":"安徽","daima":"340000000000","zidi":16}, "wjr13":{"dengji":"2","diming":"福建","daima":"350000000000","zidi":9}, "wjr14":{"dengji":"2","diming":"江西","daima":"360000000000","zidi":11}, "wjr15":{"dengji":"2","diming":"山东","daima":"370000000000","zidi":17}, "wjr16":{"dengji":"2","diming":"河南","daima":"410000000000","zidi":18}, "wjr17":{"dengji":"2","diming":"湖北","daima":"420000000000","zidi":14}, "wjr18":{"dengji":"2","diming":"湖南","daima":"430000000000","zidi":14}, "wjr19":{"dengji":"2","diming":"广东","daima":"440000000000","zidi":21}, "wjr20":{"dengji":"2","diming":"广西","daima":"450000000000","zidi":14}, "wjr21":{"dengji":"2","diming":"海南","daima":"460000000000","zidi":4}, "wjr22":{"dengji":"2","diming":"重庆","daima":"500000000000","zidi":2}, "wjr23":{"dengji":"2","diming":"四川","daima":"510000000000","zidi":21}, "wjr24":{"dengji":"2","diming":"贵州","daima":"520000000000","zidi":9}, "wjr25":{"dengji":"2","diming":"云南","daima":"530000000000","zidi":16}, "wjr26":{"dengji":"2","diming":"西藏","daima":"540000000000","zidi":7}, "wjr27":{"dengji":"2","diming":"陕西","daima":"610000000000","zidi":10}, "wjr28":{"dengji":"2","diming":"甘肃","daima":"620000000000","zidi":14}, "wjr29":{"dengji":"2","diming":"青海","daima":"630000000000","zidi":8}, "wjr30":{"dengji":"2","diming":"宁夏","daima":"640000000000","zidi":5}, "wjr31":{"dengji":"2","diming":"新疆","daima":"650000000000","zidi":15}, "wjr32":{"dengji":"2","diming":"台湾省","daima":"710000000000","zidi":0}, "wjr33":{"dengji":"2","diming":"香港特别行政区","daima":"810000000000","zidi":0}, "wjr34":{"dengji":"2","diming":"澳门特别行政区","daima":"820000000000","zidi":0} } })
"""

wjr = (data) ->
    console.log data
eval(str)

还是我自己来解析吧; 这个不是使用的JSON,而是使用的JSONP方式传回的值。 什么是JSONP: http://victor-jan.iteye.com/blog/875061

http://www.cnblogs.com/mybest/archive/2011/12/13/2285730.html

解析方法 <!DOCTYPE html> <html> <head> <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” /> <title>中国</title> <script src=“jquery-1.3.2.min.js”></script>

</head> <body> <script type=“text/javascript”> function jsonpCallback(result) { //alert(result); /for(var i in result[‘list’]) { var daima = result[‘list’][i][‘daima’]; var diming = result[‘list’][i][‘diming’];//循环输出a:1,b:2,etc. }/

	sd = "<div>"
    for (var i in result['list']) {
        sd += "<ul>"
            sd += "<li>" + result['list'][i]['daima'] + "</li>"
            sd += "<li>" + result['list'][i]['diming'] + "</li>"
        sd += "</ul>"
    }
    sd += "</div>"
    //alert(sd);
    $("html body").append(sd);
}
var JSONP=document.createElement("script");
JSONP.type="text/javascript";
JSONP.src="http://api.dangqian.com/apidiqu2/api.asp?format=json&callback=jsonpCallback&id=000000000000";
document.getElementsByTagName("head")[0].appendChild(JSONP);

</script> </body> </html>

用 jquery.ajax 的 jsonp 形式去用这接口

JSON.parse(String)JSON.stringify(Object) 可以解析一切字符串和反解析js对象(function除外)。

带换行的字符串没办法的用 JSON.parse 解析。

@think2011 纯粹是胡扯。 换行的字符是\n \r\n,都是字符。 C语言第1课就讲什么是字符。

@tulayang 抱歉,我表述的有问题。 我实践的案例中,前端发出了带换行的字符串(通过:textarea,由用户输入)到后端(c#)保存。 后端再输出给前端,输出的数据如果没有处理,大概是这样的。 JSON:

 {
msg: "
1. 打扫卫生
2. 吃饭
"
}

这个时候,JSON.parse无法解析。

@think2011 JSON.parse()可以解析。 从原理上,http传送的数据,是

"{\nmsg:\"\n1.打扫卫生\n2.吃饭\n\"\n}"

JSON.parse()解析出来是

{
    msg: "\n1.打扫卫生\n2.吃饭\n"
}

你如果没有获得期望的格式,那就是你的字符转换不正确。

@tulayang 是的,明白了,从抓包的数据来看,在发送到后端前数据是已经经过处理的。 而我的案例中出现问题,是后端没有正确处理字符串,与JSON.parse无关。

回到顶部