Parsing JSON gives few separate elements put toghether as one

Refresh

November 2018

Views

40 time

1

I get following JSON (which I have validated here):

{
    "name": "Brian",
    "agentid": "12345",
    "username": "bob",
    "passcode": "bob01",
    "call": {
        "aa": "11",
        "bb": "22",
        "cc": "33",
        "dd": "44"
    },
    "call": {
        "aa": "111",
        "bb": "222",
        "cc": "333",
        "dd": "444"
    },
    "call": {
        "aa": "1111",
        "bb": "2222",
        "cc": "3333",
        "dd": "4444"
    },
    "call": {
        "aa": "11111",
        "bb": "22222",
        "cc": "33333",
        "dd": "44444"
    }
}

When I put this through var_dump(json_decode($json, true));, it gives me:

array(5) { 
    ["name"]=> string(5) "Brian" 
    ["agentid"]=> string(5) "12345" 
    ["username"]=> string(3) "bob" 
    ["passcode"]=> string(5) "bob01" 
    ["call"]=> array(4) {
        ["aa"]=> string(5) "11111" 
        ["bb"]=> string(5) "22222" 
        ["cc"]=> string(5) "33333" 
        ["dd"]=> string(5) "44444" 
    }
} 

It looks like the first three call elements are overwritten.

How to parse this JSON and retain all call elements?

1 answers

5

Это действительно синтаксис JSON, но содержание не имеет никакого смысла. Имея больше одного ключа с тем же именем является неправильным. Вы должны сделать одного callи превратить его в JSON массив , где будет хранить все объекты:

{  
   "name":"Brian",
   "agentid":"12345",
   "username":"bob",
   "passcode":"bob01",
   "call":[  
      {  
         "aa":"11",
         "bb":"22",
         "cc":"33",
         "dd":"44"
      },
      {  
         "aa":"111",
         "bb":"222",
         "cc":"333",
         "dd":"444"
      },
      {  
         "aa":"1111",
         "bb":"2222",
         "cc":"3333",
         "dd":"4444"
      },
      {  
         "aa":"11111",
         "bb":"22222",
         "cc":"33333",
         "dd":"44444"
      }
   ]
}