How to get the JSON with duplicate keys completely in javascript

前端 未结 5 1614
后悔当初
后悔当初 2020-11-30 13:07

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? Her

5条回答
  •  生来不讨喜
    2020-11-30 13:49

    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.

提交回复
热议问题