【问题标题】:Creation of a multi-level JSON string创建多级 JSON 字符串
【发布时间】:2012-03-20 22:49:24
【问题描述】:

我想用 JS 创建一个多级 JSON 字符串。

场景

3 个国家有 5 个祖父和 3 个孩子,他们还有 3 个孩子和 5 个朋友。

我从一个看起来像这样的外部 JSON 文件中获取数据。

 {"countries":[
    {
        "name":"USA",

        "grandfathers":[
            {
                "gFName":"Steve",
                "grandfathersKid":[
                    {
                        "gFKName": "Linda",
                        "kid": [{
                            "name": "Steve JR", 
                            "friends": [{
                                "name": "Kriss|John|Martin|Steven"
                            }]
                        }
                        ]
                    }

                ]
            }
        ]
    }
]}

现在我想在一个新的 JSON 列表中存储一些人及其亲属和朋友的国家/地区,该列表看起来与外部 json 文件中的列表完全相同。我打算稍后在脚本中使用这个“自制”列表。

我对此的最初反应是

var tree = new Array();

tree = {};


var countries = new Array();

countries[0] = "canada";
countries[1] = "USA";
countries[2] = "Mexico";
countries[0][0] = "Steve"; //Lives in Canada
countries[0][0][0] = "Linda"; //Daughter of Steve
countries[0][0][0][0] = "Steve JR"; // Kid of Linda
countries[0][0][0][0][0] = "Kriss"; //Steves Friend
...


$.each(countries...function(index, value){
      tree[index].country = value;

  $.each(grandfathers...function(key, value){
      tree[index].country[key].grandfather = value;

}

等等,但这并没有给我想要的结果。我究竟做错了什么?还有比什么都拿走更有效的方法吗?

第三次编辑...

【问题讨论】:

  • var tree = new Array(); tree = {}; 可以压缩为var tree = {};。您应该提供您想要的输入和输出的示例。您的问题似乎是创建适当的结构,而不是 JSON(这只是 JSON.stringify(value))。
  • 这个例子没有意义。您正在尝试将值分配给字符串,就好像它们是数组一样。你实际上有什么输入?你真正想要什么 JSON 输出?
  • @Quentin 我已经编辑了我的答案,我有一个 JSON 输入,

标签: javascript json multidimensional-array


【解决方案1】:

这是你想做的事情吗?

var countries = $.map(oldCountries || [], function(country) {
    return {
        name: country.name,
        people: $.map(country.grandfathers || [], function(gpa) {
            return {
                name: gpa.gFName,
                children: $.map(gpa.grandfathersKid || [], function(parent) {
                    return {
                        name: parent.gFKName,
                        children: $.map(parent.kid || [], function(kid) {
                            return {
                                name: kid.name,
                                friends: kid.friends
                            };
                        })
                    };
                })
            };
        })
    };
});

我不确定如何处理 friends 节点。应该将其规范化为更有用的东西,还是您不想管它?

Fiddle 演示了这项技术。

【讨论】:

  • 这就像一个魅力,非常感谢!我暂时不要管朋友们:)
【解决方案2】:

我认为我们需要更多地了解您的要求。但我在这里看到的几件事是:

  • 您声明树并将其初始化为数组,然后立即将其重新初始化为
    空对象
  • 你这里没有创建中间节点,比如tree[index],只是假设 他们存在。
  • 您正在尝试使用点属性分配对象的country[key] 属性 访问。

你能提供国家结构和祖父结构吗?它们是嵌套的吗?

最后,您希望输出格式是什么?上面的代码暗示了,但还是有点模糊。

编辑

那么你是否试图实现这样的结构?:

var countries = [
  {
     name: "Canada",
     people: [
       {
          name: "Steve",
          children: [
            {
               name: "Linda",
               children: [
                 {
                    name: "Steve, Jr.",
                    friends: [
                      {
                         name: "Kriss"
                      }
                      //, more friends
                    ]
                 }
                 //, more grandchildren
               ]
            }
            //, more parents
          ]
       }
       //, more grandparents
     ]
   }
   //, more countries
 ];

【讨论】:

  • 我有一个 JSON 输入,看起来像这样。 {“国家”:[{“名称”:“美国”,“祖父”:[{“gFName”:“史蒂夫”,“grandfathersKid”:[{“gFKName”:“琳达”,“孩子”:[{“ name": "Steve JR", "friends": [{ "name": "Kriss|John|Martin|Steven" 我希望能够将其中一些国家/地区转换为新的 JSON,稍后我将使用它。谢谢!
  • 你的输出格式怎么样?上面的代码并没有真正指定它。
  • 没错,但最后的朋友不需要分开,应该是“Kriss|John|...”。
  • 我仍然不清楚您要做什么。您是否正在尝试将您提供的格式转换为我提供的格式?
  • 我有一个 JSON 字符串,其中包含我从外部 JSON 文件获取的上述数据,我想将 JSON 文件中的一些数据放入一个看起来与我打算稍后在脚本中使用的原点。
【解决方案3】:

可能是this jsfiddle可以帮助您入门吗?
here is an example 源自您的代码。

【讨论】:

  • @Kooilnc 是的,该解决方案有效,但我想我会使用 Scott 发布的那个。不过谢谢!
【解决方案4】:

听起来像是一项家庭作业,所以我会尽力为您指明正确的方向。我认为您混淆了对象和数组。您可以使用“国家”对象和“人”对象。一个国家对象应该有一个人对象数组,作为居民。 Person 对象可以有一个 Person 对象数组作为后代。添加一个像“addDescendant”这样的方法,它在一个人下面创建一个新人。从那里您可以根据需要构建结构。这是一些伪代码:

countries = [];
function Country(name){ this.name = name; this.population = [];}
function Person(kids){this.descendants = []; this.addDescendant = function(){...}; 
    //loop from 1 to kids and add descendants as "new Person"
}
person = new Person(3);
country1 = new Country("MyCountry1");
// now add people to country1.population
countries.push(country1);

最终的结构应该是这样的:

countries = [
    { name: "country 1",
      people: [{ name: "Steve"}, 
               {name: "Clara", descendants: [{name: "Clara's daughter"}, 
                                             {name: "Clara's son"}]
              ]}
    },
    { name: "country 2",
      people: [{}, {} ...]

    }
];

【讨论】:

  • 感谢您的帮助,但我会坚持 Scott 发布的解决方案!
猜你喜欢
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 2012-12-18
  • 1970-01-01
相关资源
最近更新 更多