1

我想做的是解析这个 JSON:http ://www.theunjust.com/test/container.php

让它在这一点上显示......

这是我当前的代码(全部被顶起)

$(document).ready(function(){
    var jsonurl = "container.php";
  $.getJSON(jsonurl, function(data) {         
    $.each(data.encompass,function(i,valu){
    var content,
    cont = '',
    tainers = valu.containers;

  $.each(tainers, function (i) {
    var tid = tainers[i].TID,
    top = tainers[i].Top,
    cat = tainers[i].Category,
    date = tainers[i].Date,
    icon = tainers[i].Icon,
    clink = tainers[i].ContainerLink,
    uid = tainers[i].UniqueID;

        cont += '<a href="' + clink + '">' + uid + '</a> ';
        $('ul').append('<li>' + tid + ' - ' + top + ' - ' + cat + ' - ' + date + ' - ' + icon + ' - ' + clink + ' - ' + uid + ' (' + cont + ')</li>');
    }); 
  }); 
}); 
});

我知道代码没有意义,这对我来说更像是一种学习体验,而不是一个功能性工具。问题可能出在 JSON 数据本身,如果是这种情况,您会推荐什么 stackoverflow Web 服务帖子?

我最终的目标是运行多个 Web 服务并将它们组合起来......这样 container.php 加载并拥有包装 div 所需的所有信息,然后 entry.php 加载并填充由生成的 div容器..

我目前正在学习 jQuery,老实说,我还没有达到这个水平,但我很快就会到达那里。

我非常感谢您的帮助,并希望您能指出我成为 JSON 解析专家的方向!

4

3 回答 3

1

containers包含单个节点。所以简单地使用

$.each(tainers, function (i, v) {
    var tid = v.TID,
    top = v.Top,
    cat = v.Category,
    date = v.Date,
    icon = v.Icon,
    clink = v.ContainerLink,
    uid = v.UniqueID;

    cont += '<a href="' + clink + '">' + uid + '</a> ';
    $('ul').append('<li>' + tid + ' - ' + top + ' - ' + cat + ' - ' + date + ' - ' + icon + ' - ' + clink + ' - ' + uid + ' (' + cont + ')</li>');
});
于 2012-08-04T03:50:35.860 回答
1

该程序展示了如何读取 json 并将数据存储到文本框中:

$(document).ready(function(){
  $.getJSON("Test.json",function(data){
       $.each(data,function(key,value){
           alert(data[key].attrib1name);
           alert(data[key].attrib2name); 
           $('input').val(data[key].enterpriseName);
           activities=data[key].activities;
           console.log(value);
       });
   });    
});
于 2014-01-30T07:49:22.440 回答
0

Don't use jQuery as a hammer for every nail for starters. This is a core JavaScript problem.

There are only two structures to parse in JavaScript. Arrays and objects.

For objects, use:

for(var x in obj){ alert(x + ':' + obj[x]) } //would alert <property name>:<property value>

For arrays use a loop like you would in PHP. In JS while loops are nice for arrays:

var i = someArray.length;
//if you don't want to loop backwards just do someArray.reverse()

while(i--){
   //do stuff to someArray[i] here
}

JSON is just an object literal in JavaScript. The simplest type of object. Just a place to stick properties. A property can be an array, a value, or another object. The only thing to watch out for is objects that reference other properties in an object (results in infinite loops when attempting to parse) which is a really dumb thing to do with JSON (not even sure that would pass validation).

于 2012-08-04T03:17:08.310 回答