1

I'm trying to decode some JSON and insert the values in MySQL. Here is my code:

$json = '
{"d":[{"Id":1059,"Name":"Alfa Romeo - 145"},{"Id":20020,"Name":"Alfa Romeo - 146"},{"Id":1060,"Name":"Alfa Romeo - 147"},{"Id":20021,"Name":"Alfa Romeo - 155"},{"Id":1061,"Name":"Alfa Romeo - 156"},{"Id":20022,"Name":"Alfa Romeo - 159"},{"Id":20023,"Name":"Alfa Romeo - 164"},{"Id":20024,"Name":"Alfa Romeo - 166"},{"Id":20025,"Name":"Alfa Romeo - 33"},{"Id":20026,"Name":"Alfa Romeo - Brera"},{"Id":20027,"Name":"Alfa Romeo - GT"},{"Id":20028,"Name":"Alfa Romeo - GTV"},{"Id":239,"Name":"Alfa Romeo - Misc"},{"Id":20029,"Name":"Alfa Romeo - Spider"},{"Id":20030,"Name":"Alfa Romeo - Sportwagon"}]}
';
$json_dec = json_decode($json);
$nr = 0;
while($nr 14) {
 $id_nr = $json_dec['d']['$nr']['Id'];
 $make = $json_dec['d']['$nr']['Name'];
}

The error that I get is

Fatal error: Cannot use object of type stdClass as array in
C:\wamp\www\get_cat.php on line 18
Adriaan
18.2k7 gold badges47 silver badges88 bronze badges
asked Nov 22, 2010 at 18:47

1 Answer 1

8

Put true as second argument for json_decode

$json_dec = json_decode($json,true);

Otherwise it will return an object instead of an array.

See here for more information: http://php.net/manual/en/function.json-decode.php

answered Nov 22, 2010 at 18:50

2 Comments

It'd be good to explain why. Setting that option converts objects into associative arrays. See php.net/manual/en/function.json-decode.php
thanks a lot !...I knew this but for some reasons I forgot it !

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.