2

this is my data ,After Json_encode()

 Array
        (
            [{"customerId":"1","customer_name":"Jon_doe","amount":"12312312","billcode":"b1231","billname":"cashbilname","billcategorycode":"1234","billcategory":"utility","month":"May","year":"2015","txcode":"10","stationid":"152","station":"Coroom","operatorcode":"1200","operator":"jame","terminal":"ter12312","txdate":"12\/2\/2015","txtime":"12:21:22_PM"}] 
    => 
        )

now i want to decode it back ,by applying json_decode() it gives the following error

json_decode() expects parameter 1 to be string, array given

any idea sugestion what to do ?

5
  • 3
    How do you encode? It looks like you doing something wrong there. Commented Jun 25, 2015 at 12:22
  • 1
    Can you please post your relevant code? Commented Jun 25, 2015 at 12:24
  • 2
    You need to pass the first item of that array to json_decode; rather than the whole array. So, json_decode($array[0]) Commented Jun 25, 2015 at 12:24
  • The error message is your answer.Did you searched at google with that error message?You would get lots of result. Commented Jun 25, 2015 at 12:27
  • 1
    json_encode should give you a string in the first place, not the Array you showed, so there's definitely a problem there. Show your code and we might be able to help Commented Jun 25, 2015 at 12:27

3 Answers 3

9

Your json must be in string, not in array

$json_string = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$json_array = json_decode($json_string);

$json_array : ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];

If your json is in array you can do :

$json_string_in_array = ['{"a":1,"b":2,"c":3,"d":4,"e":5}'];
$json_array = json_decode($json_string_in_array[0]);
Sign up to request clarification or add additional context in comments.

1 Comment

say undefined 0 index
3

json_encode() returns a string, so I don't know how you can be getting an array out of it unless you are storing it in an array yourself like:

$json = [];
$json[] = json_encode($someArray);

Instead, just store it in a non-array variable:

$jsonString = json_encode($someArray);

Then you can decode it like this:

$decodedArray = json_decode($jsonString);

Comments

0
$jsonstr = '[{"customerId":"1","customer_name":"Jon_doe","amount":"12312312","billcode":"b1231","billname":"cashbilname","billcategorycode":"1234","billcategory":"utility","month":"May","year":"2015","txcode":"10","stationid":"152","station":"Coroom","operatorcode":"1200","operator":"jame","terminal":"ter12312","txdate":"12\/2\/2015","txtime":"12:21:22_PM"}]';
$ar = json_decode($jsonstr,true); # json string to Array
$obj = json_decode($jsonstr); # json string to Object
var_dump($ar,$obj);

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.