-1

I have a bunch of values and a PHP array and I need to convert it to a JSON value for posting via CURL to parse.com

The problem is that PHP arrays are converted to JSON objects (string as key and value, vs string as just value)

I end up with

{"showtime":{"Parkne":"1348109940"}}

Rather then

{"showtime":{Parkne:"1348109940"}}

And parse complains that this is a object not an array and therefore won't accept it.

As

{"showtime":{"Parkne":"1348109940"}}

is a JSON object (key = a string)

Is there anyway to do this using json_encode? Or some solution?

4 Answers 4

6

That's the JSON spec: Object keys MUST be quoted. While your first unquoted version is valid Javascript, so's the quoted version, and both will parse identically in any Javascript engine. But in JSON, keys MUST be quoted. http://json.org


Followup:

show how you're defining your array, unless your samples above ARE your array. it all comes down to how you define the PHP structure you're encoding.

// plain array with implicit numeric keying
php > $arr = array('hello', 'there');
php > echo json_encode($arr);
["hello","there"]   <--- array

// array with string keys, aka 'object' in json/javascript
php > $arr2 = array('hello' => 'there');
php > echo json_encode($arr2);
{"hello":"there"} <-- object

// array with explicit numeric keying
php > $arr3 = array(0 => 'hello', 1 => 'there');
php > echo json_encode($arr3);
["hello","there"]  <-- array

// array with mixed implicit/explicit numeric keying
php > $arr4 = array('hello', 1 => 'there');
php > echo json_encode($arr4);
["hello","there"] <-- array

// array with mixed numeric/string keying
php > $arr5 = array('hello' => 'there', 1 => 'foo');
php > echo json_encode($arr5);
{"hello":"there","1":"foo"}   <--object
Sign up to request clarification or add additional context in comments.

5 Comments

Then why does Parse.com complain that I am sending it a object not an array?
Because an array in JS is stricturly numerically-keyed. You're sending a string key, which makes it an object. As well, {} means object in JS. Arrays use []. Your [{}] comment example is an array which has one element: an objet with key:value of park lane/111. What do you mean "can't curl"?
Okay, so why does json_encode encode my PHP array using {} ? How can I get it to use []?
If I post [{"Parkne":"111"}] manually it works fine. If I use PHP json_encode it returns {"Parkne":"111"} and I get an error. But I can't just add [] to it, as then it becomes a string +_+... so confused.
Álvaro G. Vicario's reply ended up solving my issue. I was completely wrong, it was in fact my fault. Thanks for your help!
2

Blind shot... I have the impression that your PHP data structure is not the one you want to begin with. You probably have this:

$data = array(
    'showtime' => array(
        'Parkne' => '1348109940'
    )
);

... and actually need this:

$data = array(
    array(
        'showtime' => array(
            'Parkne' => '1348109940'
        )
    )
);

Feel free to edit the question and provide a sample of the expected output.

Comments

0

You can convert your array to JSON using json_encode assuming your array is not empty you can do it like this

 $array=();
 $json = json_encode($array);
 echo $json;

Comments

0

It sounds like you need to take your single object and wrap it in an array.

Try this:

// Generate this however you normally would
$vals  = array('showtime' => array("Parkne" => "1348109940"));

$o = array(); // Wrap it up ...
$o[] = $vals; // ... in a regular array

$post_me = json_encode($o);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.