如何在JavaScript中完全使用重复键来获取JSON [英] How to get the JSON with duplicate keys completely in javascript

查看:71
本文介绍了如何在JavaScript中完全使用重复键来获取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从url中获取JSON,但在响应对象中删除了重复键。有没有办法在不删除重复密钥的情况下完全获取它?这是我的js代码

  $('document')。ready(function(){
var s = $ .getJSON(new.json);
console.log(s);
});

以下是我的new.json

  {
s:wae,
s:asd
}

但是在控制台中,我得到的json对象如下所示:

  responseJSON:Object 
s:asd

预先致谢

解决方案

JSON对象中的键应该是唯一的。否则具有该值的最后一个键通常是请求该键时出现的那个键。使用相同的键也会使区分对象的属性变得更加困难。关键的关键在于使其易于访问和可用。



为了解决这个问题,您可以:


  1. JSON

  2. 将JSON更改为包含数组

    {s:[ wae,asd]}




  3. (RFC7159)说:
    $ b


    对象中的名称应该是唯一的。

    p>在这种情况下,必须按 RFC 2119 < a>


    应该这个词或形容词推荐意思是可能有
    在特定情况下存在忽略一个特定的
    项目,但必须理解全部含义,并在选择不同的课程前仔细衡量




    RFC 7159解释了为什么独特密钥很好:



    名称全部唯一的对象可以在
    的意义上互操作,所有接收该对象的软件实现将同意
    的名称 - 值映射。当对象中的名称不是

    unique时,接收这样的对象的软件的行为是

    不可预知。许多实现仅报告名称/值对
    。其他实现报告错误或无法解析

    对象,并且某些实现报告了所有名称/值对,包括重复项在内的



    观察到JSON解析库在
    中是否存在差异,以及它们是否使调用
    软件的对象成员的顺序可见。行为不依赖于成员

    排序的实现将是可互操作的,因为它们不会受到这些差异影响的内容



    I am trying to get JSON from the url, But in the response object duplicate keys are removed. Is there any way to fetch it completely without removing the duplicate keys? Here is my js Code

    $('document').ready(function(){
        var s = $.getJSON("new.json");
        console.log(s);
    });
    

    following is my new.json

    {
    "s": "wae",
    "s": "asd"
    }
    

    But in console i am getting the json Object as follows

    responseJSON: Object
    s: "asd"
    

    Thanks in advance

    解决方案

    Keys in a JSON object should be unique. Otherwise the last key with the value is usually the one presented when requesting that key. Having keys the same also makes it more difficult to differentiate between attributes of an object. The whole point of a key is to make it accessible and available.

    To get around this you could always:

    1. Change the keys in your JSON
    2. Change the JSON to contain an array

      {"s": ["wae","asd"]}

    The JavaScript Object Notation (JSON) Data Interchange Format) (RFC7159) says:

    The names within an object SHOULD be unique.

    In this context should must be understood as specified in RFC 2119

    SHOULD This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.


    RFC 7159 explains why unique keys are good:

    An object whose names are all unique is interoperable in the sense
    that all software implementations receiving that object will agree on the name-value mappings. When the names within an object are not
    unique, the behavior of software that receives such an object is
    unpredictable. Many implementations report the last name/value pair
    only. Other implementations report an error or fail to parse the
    object, and some implementations report all of the name/value pairs,
    including duplicates.

    JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software. Implementations whose behavior does not depend on member
    ordering will be interoperable in the sense that they will not be
    affected by these differences.

    这篇关于如何在JavaScript中完全使用重复键来获取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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