不工作JSON.parse数组元素嵌套 [英] JSON.parse array nested element not working

查看:171
本文介绍了不工作JSON.parse数组元素嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误信息:


  

语法错误:JSON.parse:预计,或第4行的JSON数据列18数组元素后]


我想这是因为我有一个数组中的对象,也许这是不允许的。但我需要从经验的开发人员证实这一点。你可以把数组对象JSON?

HTML

 < D​​IV ID =EX1>< H2物实施例1< / H>< P>< / P>< H4>结果:其中; / H4>< / DIV>

的JavaScript

  VAR消息;  (函数loadAjax()
    {VAR请求;
        如果(window.XMLHtt prequest)
            {
                请求=新XMLHtt prequest();
            }
     其他{         请求=新的ActiveXObject('微软,XMLHTTP');
     }     request.open('GET','human.json');     request.onreadystatechange =功能()
     {
         如果(request.readyState == 4和&放大器; request.status == 200)
             {                 消息= request.responseText;
                 VAR OBJ = JSON.parse(消息);
                 VAR文本= obj.job [0]。医院;
                 附加('EX1',文本,'P');
             }
     }     request.send();    })();

JSON

  {
  性:{男:{FNAME:迈克尔,汤姆]},女:{FNAME:爱丽丝,凯蒂]}},
  时代:[16,80],
  工作:医学:{医院:医生},教育:{学校:老师}]
}


解决方案

这是不正确的:

 工作:医学:{医院:医生},教育:{学校:老师}]

这是正确的:

 工作:{医学:{医院:医生},教育:{学校:老师}}

注意,我改变了 [] {}

方括号用于阵列(许多单独的项目)。例如:

  {my_array:[1,2,三化]}<  - 请注意,方括号中的项目
                                         所以不分配一个值,如重点:值

花括号用于联想条目(许多键:值项目)。例如:

  {my_association:{狗:治,猫:流口水}}

请参见 JSON.org ,了解更多关于JSON的正确语法。

I am getting this error message:

SyntaxError: JSON.parse: expected ',' or ']' after array element at line 4 column 18 of the JSON data.

I think it is because I have an object within an array, and maybe that is not allowed. But I need confirmation of this from experienced developers. Can you put objects in arrays in json?

HTML

<div id="ex1"><h2>Example 1</h2><p></p><h4>results:</h4></div>

Javascript

var message;

  (function loadAjax()
    { var request;
        if (window.XMLHttpRequest)
            {
                request = new XMLHttpRequest();
            }
     else{

         request = new ActiveXObject('Microsoft,XMLHTTP');
     }

     request.open('GET','human.json');

     request.onreadystatechange = function()
     {
         if(request.readyState == 4 && request.status == 200)
             {

                 message = request.responseText;
                 var obj = JSON.parse(message);
                 var text = obj.job[0].hospital;
                 attach('ex1',text,'p');
             }
     }

     request.send();

    })(); 

JSON

{
  "sex":{"male":{"fname":["Michael","Tom"]} ,"female":{"fname":["Alice","Katie"]}},
  "age":[16,80],
  "job":["medical":{"hospital": "doctor"}, "education":{"school":"teacher"} ]
}

解决方案

This is incorrect:

"job":["medical":{"hospital": "doctor"}, "education":{"school":"teacher"} ]

This is correct:

"job": {"medical": {"hospital": "doctor"}, "education": {"school": "teacher"} }

Notice I changed the [] to {}

Square brackets are used for arrays (many individual items). Example:

{ "my_array": [1, 2, "three"] }      <-- Notice that the items in square brackets
                                         so not assign a value such as "key": "value"

Curlies are used for associative entries (many "key": "value" items). Example:

{ "my_association": { "dogs": "rule", "cats": "drool" } }

Please refer to JSON.org to learn more about the proper syntax for JSON.

这篇关于不工作JSON.parse数组元素嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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