这样一个JSON字符串怎么解析?

时间:2021-12-24 20:24:09

[{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/499229@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/662417@","programlength":0,"seriesid":"true","tags":"","typeid":3}],
"programtype":"1",
"preference":"言情剧|偶像剧",
"total_results":"10"}]


这个JSON是这样的,本身是以Map在后台格式化的,然后里面第一个key是prolist值是List
剩下的programtype、preference、total_results都是字符串,
我现在要在前台用JS或jQuery将prolist里的值循环显示出来,后面那3个字符串也单独显示出来
可是我怎么也取不到值

$.getJSON("ppshow", function(data){
    $.each(data, function(i, item){
        alert(i);
    });
});

我这样写只给弹了个0出来了,难道还是Map不能这样取么?

6 个解决方案

#1


本来就只有一条记录啊


var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/499229@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/662417@","programlength":0,"seriesid":"true","tags":"","typeid":3}],
"programtype":"1",
"preference":"言情剧|偶像剧",
"total_results":"10"}];

alert( json.length )
for( var x in json[0] ){
alert( json[0][x] )
}

#2


引用 1 楼  的回复:
本来就只有一条记录啊

JScript code

var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","p……

谢谢,明白了,Map用each遍历不了
可为什么我刚才用这种写法取不到呢?
[JScript]
data['prolist']['canrecommend']
[/JScript]
这样就是unfinder

#3



var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/499229@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/662417@","programlength":0,"seriesid":"true","tags":"","typeid":3}],
"programtype":"1",
"preference":"言情剧|偶像剧",
"total_results":"10"}];
alert( json[0]['prolist'][0]['canrecommend'] )

#4


var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/499229@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/662417@","programlength":0,"seriesid":"true","tags":"","typeid":3}],
"programtype":"1",
"preference":"言情剧|偶像剧",
"total_results":"10"}];



获取时用这json[0].prolist[0].canrecommend,就能获取到第一个actors里面的canrecommend对应的值了。
已经帮你测试过了。
具体要哪个对象和逻辑处理就你自己看着办了,解析数据用findbug看下就很快能解析了

#5


引用 3 楼  的回复:
JScript code

var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":……


原来一直没关注这个[],
这个字符串最外边是个[],用下标取的,于是data的长度为1
然后里面嵌套了一个List,list里的元素也是[]包起的
谢谢啦,以前没用过JSON,学习了!
分全给你!

#6


引用 4 楼  的回复:
var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":0,"seriesid":"……


你的也对,谢谢,给分

#1


本来就只有一条记录啊


var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/499229@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/662417@","programlength":0,"seriesid":"true","tags":"","typeid":3}],
"programtype":"1",
"preference":"言情剧|偶像剧",
"total_results":"10"}];

alert( json.length )
for( var x in json[0] ){
alert( json[0][x] )
}

#2


引用 1 楼  的回复:
本来就只有一条记录啊

JScript code

var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","p……

谢谢,明白了,Map用each遍历不了
可为什么我刚才用这种写法取不到呢?
[JScript]
data['prolist']['canrecommend']
[/JScript]
这样就是unfinder

#3



var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/499229@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/662417@","programlength":0,"seriesid":"true","tags":"","typeid":3}],
"programtype":"1",
"preference":"言情剧|偶像剧",
"total_results":"10"}];
alert( json[0]['prolist'][0]['canrecommend'] )

#4


var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/499229@","programlength":0,"seriesid":"true","tags":"","typeid":3},
 {"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/662417@","programlength":0,"seriesid":"true","tags":"","typeid":3}],
"programtype":"1",
"preference":"言情剧|偶像剧",
"total_results":"10"}];



获取时用这json[0].prolist[0].canrecommend,就能获取到第一个actors里面的canrecommend对应的值了。
已经帮你测试过了。
具体要哪个对象和逻辑处理就你自己看着办了,解析数据用findbug看下就很快能解析了

#5


引用 3 楼  的回复:
JScript code

var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":……


原来一直没关注这个[],
这个字符串最外边是个[],用下标取的,于是data的长度为1
然后里面嵌套了一个List,list里的元素也是[]包起的
谢谢啦,以前没用过JSON,学习了!
分全给你!

#6


引用 4 楼  的回复:
var json = [{"prolist":
[{"actors":"","addedtime":"","canrecommend":false,"director":"","isadvertisement":false,"keywords":"","name":"","programid":"Umai:SERI/661338@","programlength":0,"seriesid":"……


你的也对,谢谢,给分