Javascript 如何向JSON数组添加级别?

Javascript 如何向JSON数组添加级别?,javascript,json,Javascript,Json,我有一个包含7000个对象的JSON数组,它们都在一个父对象之下。我想在前面添加两个级别,一个是年级别,然后是月级别 目前,我的结构如下所示: { //apod is single representation of main parent "apod" : { //below is a unique identifier with nested data "-KgpW6owlA2YGwKFj2V-" : { "date" : "1995-06-16",

我有一个包含7000个对象的JSON数组,它们都在一个父对象之下。我想在前面添加两个级别,一个是年级别,然后是月级别

目前,我的结构如下所示:

{
//apod is single representation of main parent
  "apod" : {
//below is a unique identifier with nested data
      "-KgpW6owlA2YGwKFj2V-" : {
          "date" : "1995-06-16",
          "explanation" : "If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star.",
          "gsHdUrl" : "https://www.foo.com",
          "gsThumbnailUrl" : "https://www.foo.com",
          "hdurl" : "https://apod.nasa.gov/apod/image/e_lens.gif",
          "media_type" : "image",
          "service_version" : "v1",
          "title" : "Neutron Star Earth",
          "url" : "https://apod.nasa.gov/apod/image/e_lens.gif"
      },
//below is another unique identifier with nested data
      "-KgpW7fV9laX8YL30glD" : {
          "date" : "1995-06-20",
          "explanation" : "Today's Picture: June 20, 1995    The Pleiades Star Cluster  Picture Credit: Mount Wilson Observatory  Explanation:  The Pleiades star cluster, M45, is one of the brightest star clusters visible in the northern hemisphere. It consists of many bright, hot stars that were all formed at the same time within a large cloud of interstellar dust and gas. The blue haze that accompanies them is due to very fine dust which still remains and preferentially reflects the blue light from the stars.   We keep an archive of previous Astronomy Pictures of the Day.   Astronomy Picture of the Day is brought to you by  Robert Nemiroff and  Jerry Bonnell . Original material on this page is copyrighted to Robert J. Nemiroff and Jerry T. Bonnell.",
          "gsHdUrl" : "https://www.foo.com",
          "gsThumbnailUrl" : "https://www.foo.com",
          "hdurl" : "https://apod.nasa.gov/apod/image/pleiades2.gif",
          "media_type" : "image",
          "service_version" : "v1",
          "title" : "Pleiades Star Cluster",
          "url" : "https://apod.nasa.gov/apod/image/pleiades2.gif"
      }
}
我要将其转换为的内容如下所示:

{
"apod": {
    "1995": {
        "06": {
            "-KgpW6owlA2YGwKFj2V-": {
                "date": "1995-06-16",
                "explanation": "If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star.",
                "gsHdUrl": "https://www.foo.com",
                "gsThumbnailUrl": "https://www.foo.com",
                "hdurl": "https://apod.nasa.gov/apod/image/e_lens.gif",
                "media_type": "image",
                "service_version": "v1",
                "title": "Neutron Star Earth",
                "url": "https://apod.nasa.gov/apod/image/e_lens.gif"
            },
            "-KgpW7fV9laX8YL30glD": {
                "date": "1995-06-20",
                "explanation": "Today's Picture: June 20, 1995    The Pleiades Star Cluster  Picture Credit: Mount Wilson Observatory  Explanation:  The Pleiades star cluster, M45, is one of the brightest star clusters visible in the northern hemisphere. It consists of many bright, hot stars that were all formed at the same time within a large cloud of interstellar dust and gas. The blue haze that accompanies them is due to very fine dust which still remains and preferentially reflects the blue light from the stars.   We keep an archive of previous Astronomy Pictures of the Day.   Astronomy Picture of the Day is brought to you by  Robert Nemiroff and  Jerry Bonnell . Original material on this page is copyrighted to Robert J. Nemiroff and Jerry T. Bonnell.",
                "gsHdUrl": "https://www.foo.com",
                "gsThumbnailUrl": "https://www.foo.com",
                "hdurl": "https://apod.nasa.gov/apod/image/pleiades2.gif",
                "media_type": "image",
                "service_version": "v1",
                "title": "Pleiades Star Cluster",
                "url": "https://apod.nasa.gov/apod/image/pleiades2.gif"
            }
        },
        "07": {}
    },
    "1996": {}
}
}
我想保留
键->对象
映射。通过这个循环并重组它的最佳方式是什么?我有点不知所措,因为我没有花那么多时间编辑/重组JSON

多谢各位

{
    "2015": {
        "01": [{...}, {...}, ...],
        "02": [{...}, ...],
        ...
        "12": [...]
    },
    "2016": {
        ...
    }
}
要通过PHP循环,您可以:

foreach ($data as $year => $monthes) {
    foreach ($monthes as $month => $objects) {
        foreach ($objects as $object) {
            // do something with $year, $month and $object
        }
    }
}

您拥有的是对象,而不是数组。不管怎样,我把事情说了出来

function convert( json ){

  // `data` is a JavaScript object we get from parsing
  // json. For simplicity we remove the `apod` property
  // and add it prior to returning.
  let data;
  try {
    data = JSON.parse(json).apod;
  }
  catch(e){
    alert("invalid json!");
  }

  // Create a new object to store the mapped objects.
  // We will convert this to a json string in the end.
  const result = {};

  // Iterate all properties.
  const keys = Object.keys(data);
  for( let key of keys ){

    // Parse month and year.
    const [year, month] = data[key].date.split("-", 2);

    // If there hadn't been an occurrence of this year,
    // introduce an object.
    if( !result[year] )
      result[year] = {};

    // If there hadn't been an occurrence of this month
    // in this year, introduce an object.
    if( !result[year][month] )
      result[year][month] = {};

    // Add element to current year and month.
    result[year][month][key] = data[key];
  }

  // Convert and return the mapped object as json.
  return JSON.stringify({ apod: result });

}

您使用什么工具来和JSON交互?现在我刚刚在Atom中打开了它,我正在使用Python将它存储到Firebase中。除此之外,我不确定如何循环所有内容。我假设值,例如
-KgpW6owlA2YGwKFj2V-
是对象的唯一键?您想保持
key->Object
映射的原样还是将key嵌入相应的对象中?
apod
的值只是Json主父级的一个表示,还是您也允许使用其他值?你能澄清一下你的具体要求吗?@RZet,谢谢。我更新了我原来的帖子来澄清我的问题。到目前为止,我一直在使用
lodash
&
moment
,使用Javascript,但我不知道如何从嵌套对象中提取数据。是
“07”:{}
“1996”:{}
打字错误?谢谢你的回答,如果我不清楚,很抱歉,但我正在尝试找出一种简单的方法来循环所有数据,我理解它需要如何组织。我编辑了我的OP以澄清。PHP示例添加了很多Hanks,这正是我所需要的。我很感激!