解析一个复杂的阵列的getJSON jQuery的 [英] Parsing a complicated array with GetJSON Jquery

查看:146
本文介绍了解析一个复杂的阵列的getJSON jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR:

  1. 在入门这个问题简化后得到它的工作,并继续它的一些在这里。
  2. 我要GETJSON数组
  3. 正确,并为每个内部数组将其放置在相应的DIV格式吧。
  4. ??
  5. 在它的工作原理。

这是从这个问题,以简化和继续。

我需要从一个数组一些复杂的JSON数据。有了它,我需要的标题和它的价值。为什么我这样做的原因是因为我会知道数组被称为,但不是被它里面所产生的数据。

可以说,这个新的数组如下所示:

  {天:
[{天:星期天,时间:10.00},
 {天:星期一,时间:12.00},
 {天:星期二,时间:09.00},
 {天:日,时间:10.00},
 {天:日,时间:02.00},
 {天:星期五,时间:05.00},
 {天:星期六,时间:08.00}
]}
 

固定拜(太富兰琛)

我需要它来获取星期日及放大器; 10.00以及同所有其他人。

我的code,此刻来解析这个如下:

  $。的getJSON(URL,
    功能(数据){
      $每个(数据,功能(I,项目){
      $('。testfield)追加。('< P>'+项目+'< / P>');
      });
    });
 

而对每个日期的附加时候,它会分析数据如下:

 周日,周一,周二,周三,周四,周五,周六
 

通过加入天阵萤火虫不再将其识别为一个JSON字符串的时代。所以我猜我已经格式化的东西错在这里。另外,我每天需要和放大器;时间是在一个新的生产线我认为

  $('testfield。')追加。('< P>'+项目+'< / P>'+'< BR />');
 

就申请了每一个到一个新的生产线,但没有奏效。

  1. 如何让每一天或项目要上一个新行?
  2. 如何获得$的getJSON正确一天,它的值来分析数据,到一个div?
解决方案

试试这个(格式化JSON - 下一次检查与JSON的 JSONLint ):

  {
    天:
        {
            星期天:10.00,
            星期一:12.00,
            星期二:09.00,
            星期三:10.00,
            星期四:02.00,
            星期五:05.00,
            星期六:08.00
        }
    ]
}
 

脚本的工作吧:

  $ .getJSON(URL,功能(数据){
  $每个(data.Days [0],功能(键,值){
   $('。testfield)追加('< P>'+键+:+价值+'< / P>');
  });
 });
 

TLDR:

  1. Started with this question simplified it after got some of it working and continuing it here.
  2. I need to 'GET' the JSON array
  3. Format it correctly and for each within the array place it in the corresponding DIV.
  4. ??
  5. It works.


This is a followup from this question to simplify and continue.

I need to some complicated JSON data from an array. With it I need the title and it's value. The reason why I am doing this is because I will know what the array is called but not the data that is being generated inside it.

Lets say this new array looks as follows:

    {"Days":
[{"day": "Sunday", "time": "10.00"},
 {"day": "Monday", "time": "12.00"},
 {"day": "Tuesday", "time": "09.00"},
 {"day": "Wednesday", "time": "10.00"},
 {"day": "Thursday", "time": "02.00"},
 {"day": "Friday", "time": "05.00"},
 {"day": "Saturday", "time": "08.00"}
]}

Fixed thanks to (Matthew Flaschen)

I would need it to get Sunday & 10.00 as well as the same for all of the others.

My code to parse this at the moment is as follows:

$.getJSON(url,
    function(data){
      $.each(data, function(i,item){
      $('.testfield').append('<p>' + item + '</p>');
      });
    });

Without the added times on each date it will parse the data as follows:

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

With the times added to the days in the array Firebug no longer recognizes it as a JSON string. So I am guessing I have formatted something wrong here. Also, I need each day & time to be on a new line I thought that

$('.testfield').append('<p>' + item + '</p>' + '<br />');

Would have applied each one to a new line but that did not work.

  1. How do I get each day or item to be on a new line?
  2. How do I get the $getjson to parse the data correctly day and its value, into a div?

解决方案

Try this (reformatted JSON - next time check your JSON with JSONLint):

{
    "Days": [
        {
            "Sunday": "10.00",
            "Monday": "12.00",
            "Tuesday": "09.00",
            "Wednesday": "10.00",
            "Thursday": "02.00",
            "Friday": "05.00",
            "Saturday": "08.00" 
        }
    ]
}

Script to work with it:

 $.getJSON( url, function(data){
  $.each(data.Days[0], function(key,value){
   $('.testfield').append('<p>' + key + ' : ' + value + '</p>');
  });
 });

这篇关于解析一个复杂的阵列的getJSON jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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