JSONObject와 JSONArray의 차이점 링크 를

Google을 잠시 살펴본 후 구문의 관점에서 차이점을 설명하는 이 링크 를 찾았습니다 .

프로그래밍 시나리오에서 하나가 다른 것보다 선호되는시기는 언제입니까?



답변

Android에서 JSON 데이터로 작업 할 때 JSONArray배열 대괄호로 시작하는 JSON을 구문 분석 하는 데 사용 합니다. JSON의 배열은 관련 항목 모음을 구성하는 데 사용됩니다 (JSON 개체 일 수 있음).
예를 들면 :[{"name":"item 1"},{"name": "item2} ]

반면에 JSONObject중괄호로 시작하는 JSON을 다룰 때 사용 합니다. JSON 개체는 일반적으로 한 항목과 관련된 키 / 값 쌍을 포함하는 데 사용됩니다. 예를 들면 :{"name": "item1", "description":"a JSON object"}

물론 JSON 배열과 객체는 서로 중첩 될 수 있습니다. 이에 대한 일반적인 예는 쿼리와 일치하는 항목의 배열과 함께 일부 메타 데이터가 포함 된 JSON 개체를 반환하는 API입니다.

{"startIndex": 0, "data": [{"name":"item 1"},{"name": "item2"} ]}


답변

차이점은 (Hash) Map vs List와 같습니다.

JSONObject :

  • 명명 된 값 (키-> 값 쌍, 튜플 또는 호출하려는 모든 항목)을 포함합니다.
    • 처럼 {ID : 1}
  • 요소의 순서는 중요하지 않습니다.
    • 의 JSONObject는 {id: 1, name: 'B'}입니다 {name: 'B', id: 1}.

JSONArray :

  • 시리즈 값만 포함
    • 처럼 [1, 'value']
  • 가치의 순서가 중요합니다
    • 의 배열이 [1,'value']다음과 같지 않습니다.['value',1]

JSON Object --> { "":""}

JSON Array --> [ , , , ]

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}


답변

프로그래밍 방식으로 가장 잘 이해합니다.

구문이 {}다음과 같을 때JsonObject

구문이 []다음과 같을 때JsonArray

A JSONObject는 .NET Framework에서 요소로 표현할 수있는 JSON과 유사한 객체입니다 JSONArray. JSONArray하나 (또는 ​​여러 개)를 포함 할 수 있습니다.JSONObject

이것이 당신에게 도움이되기를 바랍니다!


답변

나는 항상 객체를 사용하며, 더 쉽게 확장 할 수 있지만 JSON 배열은 그렇지 않습니다. 예를 들어 원래는 일부 데이터가 json 배열로 있었는데, 그 위에 상태 헤더를 추가해야했는데, 객체에 데이터를 중첩하지 않는 한 약간 멈췄습니다. 유일한 단점은 생성 / 파싱의 복잡성이 약간 증가한다는 것입니다.

그래서 대신

[datum0, datum1, datumN]

당신은

{data: [datum0, datum1, datumN]}

나중에 더 추가 할 수 있습니다 …

{status: "foo", data: [datum0, datum1, datumN]}


답변

더 쉬운 방법으로 이해하기 위해 JSON 객체와 JSON 배열의 차이점은 다음과 같습니다.

표 형식 차이 링크 : https://i.stack.imgur.com/GIqI9.png

JSON 배열

1. Arrays in JSON are used to organize a collection of related items
   (Which could be JSON objects)
2.  Array values must be of type string, number, object, array, boolean or null
3.  Syntax:
           [ "Ford", "BMW", "Fiat" ]
4.  JSON arrays are surrounded by square brackets [].
    **Tip to remember**  :  Here, order of element is important. That means you have
    to go straight like the shape of the bracket i.e. straight lines.
   (Note :It is just my logic to remember the shape of both.)
5.  Order of elements is important. Example:  ["Ford","BMW","Fiat"] is not
    equal to ["Fiat","BMW","Ford"]
6.  JSON can store nested Arrays that are passed as a value.

JSON 객체

1.  JSON objects are written in key/value pairs.
2.  Keys must be strings, and values must be a valid JSON data type (string, number,
    object, array, boolean or null).Keys and values are separated by a colon.
    Each key/value pair is separated by a comma.
3.  Syntax:
         { "name":"Somya", "age":25, "car":null }
4.  JSON objects are surrounded by curly braces {}
    Tip to remember : Here, order of element is not important. That means you can go
    the way you like. Therefore the shape of the braces i.e. wavy.
    (Note : It is just my logic to remember the shape of both.)
5.  Order of elements is not important.
    Example:  { rollno: 1, firstname: 'Somya'}
                   is equal to
             { firstname: 'Somya', rollno: 1}
6.  JSON can store nested objects in JSON format in addition to nested arrays.


답변

JSON이 {}로 시작하면 Object JSON 객체
이고 []로 시작하면 Array JOSN Array입니다.

JSON 배열은 여러 개체로 구성 될 수 있으며이를 개체 배열이라고합니다.


답변

이전의 모든 답변은 귀하의 질문에 대한 통찰력이 있습니다. 나는이 SO 스레드를 찾기 1 분 전에 당신과 같은 혼란을 겪었습니다. 몇 가지 답변을 읽은 후 얻은 결과는 다음과 같습니다 . JSONObject는 배열의 요소 인 JSONArray로 표현할 수있는 JSON과 유사한 객체입니다. 즉, JSONArray는 하나 이상의 JSONObject를 포함 할 수 있습니다.