jQuery读取嵌套的JSON [英] jquery reading nested json

查看:76
本文介绍了jQuery读取嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下json,如下所示.我正在尝试读取值TOP1,TOP2.我不太确定如何做到这一点.

我正在使用以下..但这只是给我一个对象,该对象具有用于TOP1和TOP2的嵌套对象.如何获取值TOP1和TOP2?

$.getJSON('http://localhost/data/menufixed.json',
    function(data) {            
        $.each(data, function(entryIndex, entry) {
            var html = '<li class="top-level">';

        });
    });

以及下面的数据

{
"actions" : [
    {
        "action": "TOP1",
        "subaction": [
            {
                "name": "A" 
            },
            {
                "name": "B" 
            },
            {
                "name": "C" 
            } 
        ] 
    },
    {
        "action": "TOP2",
        "subaction": [
            {
                "name": "X" 
            },
            {
                "name": "Y" 
            } 
        ] 

解决方案

您似乎想遍历.actions,因此请更改此内容:

$.each(data, function(entryIndex, entry) {
  var html = '<li class="top-level">';
});

对此:

$.each(data.actions, function(entryIndex, entry) {
  var html = '<li class="top-level">' + this.action + '</li>';
});

使用data.actions,您现在遍历该对象数组,而那些对象是具有.action属性的对象,例如:"TOP1""TOP2". /p>

I have the following json as seen below. I'm trying to read the values TOP1, TOP2. I'm a little unsure of how to do this.

I'm using the following .. But that just gets me an object which has the nested objects for TOP1 and TOP2. How do I get the values TOP1 and TOP2 ??

$.getJSON('http://localhost/data/menufixed.json',
    function(data) {            
        $.each(data, function(entryIndex, entry) {
            var html = '<li class="top-level">';

        });
    });

And the data below

{
"actions" : [
    {
        "action": "TOP1",
        "subaction": [
            {
                "name": "A" 
            },
            {
                "name": "B" 
            },
            {
                "name": "C" 
            } 
        ] 
    },
    {
        "action": "TOP2",
        "subaction": [
            {
                "name": "X" 
            },
            {
                "name": "Y" 
            } 
        ] 

解决方案

It looks like you want to loop though the .actions, so change this:

$.each(data, function(entryIndex, entry) {
  var html = '<li class="top-level">';
});

To this:

$.each(data.actions, function(entryIndex, entry) {
  var html = '<li class="top-level">' + this.action + '</li>';
});

Using data.actions you're now looping through that array of objects, and those objects are the ones with the .action property, for example: "TOP1" and "TOP2".

这篇关于jQuery读取嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆