1

When I do json_encode from this array:

array('aps' => array(
                     'alert' => array(
                                     'param1'=>array('string'), 
                                     'param2'=>'string' )));

I'm getting this JSON object:

{
    "aps" :    {
      "alert" :         {
            "param1" :             {
                "0" : "string"
            },
            "param2" : "string"
        }
    }
}

Instead of

{
    "aps" :    {
      "alert" :         {
            "param1" :             
                ["string"],

            "param2" : "string"
        }
    }
}

It seems to work right when the param1 array is not a single item. How could I fix it? The Json is created from a third party bundle, so I should format the array in PHP so that I could get the correct JSON on json_encode (param1 as a list).

Manolo
  • 24,020
  • 20
  • 85
  • 130
  • @ShankarDamodaran: What's wrong with it apart from the missing quotes, which are really beside the point? – Jon May 07 '14 at 10:32
  • @ShankarDamodaran - I don't want the `0` index from the single numeric array (`param1`) – Manolo May 07 '14 at 10:33
  • 2
    [I cannot reproduce](http://3v4l.org/6TYcm). I suspect you rewrote your code and output for the question—you should post some *real* code we can test. – Álvaro González May 07 '14 at 10:34
  • 1
    @ManoloSalsas: Your expected output is what you should be getting, and indeed [what I am getting](http://ideone.com/xomVST). – Jon May 07 '14 at 10:35
  • @Jon - It's really strange... – Manolo May 07 '14 at 10:41
  • @ÁlvaroG.Vicario - Now it is right, but I don't understand why I'm not getting that JSON. – Manolo May 07 '14 at 10:42
  • 1
    Sorry but what you claim to be your actual JSON does not even pass the [JSONLint](http://jsonlint.com/) validator. I'm sure you honestly think you've posted the relevant code but facts suggest is isn't the case. – Álvaro González May 07 '14 at 11:10
  • @ÁlvaroG.Vicario - Yes, sorry. I was missing a coma. I think now it is right. – Manolo May 07 '14 at 11:13
  • 1
    No, it isn't. The problem is that you are typing manually some code that you wrongly think that illustrates the issue. You should be using the clipboard to post **real code** that you've tested yourself. – Álvaro González May 07 '14 at 11:16
  • @ÁlvaroG.Vicario - Sorry, validated and edited. – Manolo May 07 '14 at 11:30

1 Answers1

2

See this answer: https://stackoverflow.com/a/11722121/1466341

And more info here: http://www.php.net/manual/en/function.json-encode.php

Note: When encoding an array, if the keys are not a continuous numeric sequence starting from 0, all keys are encoded as strings, and specified explicitly for each key-value pair.

In your case everything should work fine, but I guess you have simplified your PHP array in this example. So, idea is simple - if your PHP array don't have all keys sequential, then json_encode will treat them as keys of object.

Community
  • 1
  • 1
DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • I knew that. But I supposed that if there is only one key, it is treating it as an associative one. But I think that I was wrong with it. – Manolo May 07 '14 at 11:46
  • Yeah, that's strange. Maybe that depends on your PHP version. – DarkSide May 07 '14 at 22:20