0%

jQuery 读取JSON 指定内容

jQuery代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$.ajax({
type:"GET",
url: "http://www.ttwinbug.com/json/json.php",
dataType:"json",
jsonp:'jsoncallback',
global: false,
success:function(data){
//json文件中已product为主要项
var productList = data.product;
//str变量为需要读取的指定id
$.each(str, function(y, yu){
for(var i = 0; i<productList.length; i++){
if(str[y] == productList[i].idlist.ID){
title = productList[i].idlist.post_title;
content = productList[i].idlist.post_content;
attribe[0]=productList[i].idlist.attrib.recording_media;
attribe[1]=productList[i].idlist.attrib.image_sensor_size;
attribe[2]=productList[i].idlist.attrib.aspect_ratio;
attribe[3]=productList[i].idlist.attrib.lens_mount;
$.each(attribe, function(zindex, zvalue){
if(!attribe[zindex]){
attribe[zindex] = "-";
}
});
jsonData += "product:" + title + ",<br/> introduce:" + content + ",<br/> attribe:" + attribe + "<br/><br/><br/>";
}
}
});

alert(jsonData);
$('.cp_title').html(jsonData);
//以下部分为全部读取json里的内容
/*for(var i=0; i<productList.length; i++){

var title = productList[i].idlist.post_title;
var content = productList[i].idlist.post_content;
var attribe = Array();
attribe[0]=productList[i].idlist.attrib.recording_media;
attribe[1]=productList[i].idlist.attrib.image_sensor_size;
attribe[2]=productList[i].idlist.attrib.aspect_ratio;
attribe[3]=productList[i].idlist.attrib.lens_mount;
$.each(attribe, function(zindex, zvalue){
if(!attribe[zindex]){
attribe[zindex] = "-";
}
});

jsonData += "product:" + title + ",<br/> introduce:" + content + ",<br/> attribe:" + attribe + "<br/><br/><br/>";
}*/


}
});

JSON文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{
"product":[
{"idlist":{
"ID":"20",
"post_title":"EOS 1200D",
"post_content":"Canon makes DSLR photography possible for everybody with the advent of EOS 1200D. This latest entry model DSLR with its host of user-friendly features and creative filters, enable first-time DSLR users to capture attractive images with sharp details and beautiful background blur.<br>18 megapixel APS-C size CMOS sensor<br>3-inch large-screen LCD monitor<br>Standard ISO 100 – 6400 (expandable to 12800)",
"post_date":"2014-06-12 02:09:19",
"post_name":"eos-1200d",
"meta_value":"22",
"thumb_img":"2014/06/eos-1200d-b1.png",
"attrib":{
"recording_media":"SD memory card, SDHC memory card, SDXC memory card",
"image_sensor_size":"Approx. 22.3 x 14.9mm",
"compatible_lenses":"Canon EF lenses (including EF-S lenses) * Excluding EF-M lenses (35mm-equivalent focal length is approx. 1.6 times the lens focal length)",
"effective_pixels":"Approx. 18.0 megapixels",
"aspect_ratio":"3:2",
"dust_delete_feature":"Dust Delete Data appending, Clean manually",
"weight":"Approx. 45g / 1.6oz."}}
},
{"idlist":{
"ID":"23",
"post_title":"EOS 1100D",
"post_content":"The EOS 1100D is set to attract more casual users to adopt DSLRs with the availability of four stylish colours. It includes a 2.7-inch, 230,000 dots Wide Angle Viewing TFT LCD monitor, a redesigned control layout that supports one-handed use, Scene Intelligent Auto, Basic+ for enhancing colours and Feature Guide for a quick reference to the features and benefits of the EOS 1100D.<br>12.2 megapixel APS-C size CMOS sensor<br>9-point cross-type AF system<br>ISO 100-3200, expandable to 6400",
"post_date":"2014-06-12 02:58:31",
"post_name":"eos-1100d",
"meta_value":"24",
"thumb_img":"2014/06/eos1100dkit-m91.png",
"attrib":{
"recording_media":"SD memory card, SDHC memory card, SDXC memory card",
"image_sensor_size":"Approx. 22.0 x 14.7mm",
"compatible_lenses":"Canon EF lenses (including EF-S lenses) (35mm-equivalent focal length is approx. 1.6 times the lens focal length)",
"lens_mount":"Canon EF mount",
"effective_pixels":"Approx. 12.20 megapixels",
"aspect_ratio":"3:2",
"dust_delete_feature":"Manual, Dust Delete Data appended",
"weight":"Approx. 495g / 17.5oz. (CIPA Guidelines) Approx. 450g / 15.9oz. (Body only)"}}
},
{"idlist":{
"ID":"39",
"post_title":"EOS 600D",
"post_content":"The EOS 600D is powered by an 18-megapixel high image quality CMOS sensor and the powerful DIGIC 4 image processor. Features like Scene Intelligent Auto, Basic+ and Feature Guide make it simple to achieve full control over creative functions. The advanced Creative are integrated into the EOS 600D, as well as Full HD video recording and Video Snapshot.<br>18.0 megapixel APS-C size CMOS sensor<br>Full HD recording in 24, 25 and 30fps<br>ISO 100-6400, expandable to 12800",
"post_date":"2014-06-30 08:01:41",
"post_name":"eos-600d",
"meta_value":"40",
"thumb_img":"2014/06/eos600d-body-b1.png",
"attrib":{
"recording_media":"SD memory card, SDHC memory card, SDXC memory card",
"image_sensor_size":"Approx. 22.3 x 14.9mm",
"compatible_lenses":"Canon EF lenses (including EF-S lenses)",
"lens_mount":"Canon EF mount",
"effective_pixels":"Approx. 18.00 megapixels"}}
},
{"idlist":{
"ID":"41",
"post_title":"EOS 650D ",
"post_content":"The EOS 650D promises unparalleled performance with the world's* first intuitive touch screen (vari-angle) interface for set-shoot-view convenience. Its Auto-focus (AF) System is precise with a new 9-point all cross-type AF backed by a new Hybrid CMOS AF System. AF speeds are dramatically increased while focusing on live view and movie shooting modes.<br><br>* as of 6 June 2012<br>ISO 100-12800, expandable to 25600<br>Handheld Night Scene<br>Continuous AF in Movie Mode",
"post_date":"2014-06-30 08:07:39",
"post_name":"eos-650d",
"meta_value":"42",
"thumb_img":"2014/06/eos650d-b6.png",
"attrib":{
"recording_media":"SD memory card, SDHC memory card, SDXC memory card Compatible with UHS-I",
"image_sensor_size":"Approx. 22.3 x 14.9mm",
"effective_pixels":"Approx. 18.00 megapixels",
"aspect_ratio":"3:2",
"recording_pixels":"L (Large): Approx. 17.90 megapixels (5184 x 3456)",
"recording_pixels":"M (Medium): Approx. 8.00 megapixels (3456 x 2304)"}}
}
]}