views:

281

answers:

1

hi,

i have following json format

{
    "status": "ACTIVE",
    "result": false,
    "isworking": false,
    "margin": 1,
    "employee": {
        "111": {
            "val1": 5.7000000000000002,
            "val2": "9/2",
            "val3": 5.7000000000000002 
        },
        "222": {
            "val1": 31.550000000000001,
            "val2": "29/1",
            "val3": 31.550000000000001 
        } 
    }
} 

how the problem is when i am trying to decode above json response in php using

json_decode($res,true) { true param for associative array } 

i am getting following result as few fields like "result":false is not "result":"false" i.e. at many of the places doubles quotes are missing in values of json. see in val1 and val3 fields

resultant data after decoding in php (associative array)

Array ( 
        [status] => > ACTIVE [result] => > [isworking] => > [margin] => > 1 [employee] => > Array (
        [111] => > Array ( 
            [val1] => > 5.7 [val2] => > 9/2 [val3] => > 5.7 
        ) 
        [222] => > Array ( 
            [val1] => > 31.55 [val2] => > 29/1 [val3] => > 31.55 
        ) 
    ) 
) 

please help me on how would i insert double quotes in values ? Thanks

+2  A: 

Actually, false is valid boolean value in json, and so json_decode it properly.

If you want "false" as string, your original json should be "false" too.

S.Mark
but i don't have control over json format as i am fetching it from external server
hunt
actually, you don't really need to change json itself to `"false"`, you just need to use `false` instead of `"false"` in your codes, and if you test for `true`, you could just do `if (json["result"]) {...}` or `if (json["isworking"]){ ... }`
S.Mark
ya thats true by i can replace false with "false" but the problem is how would i do that in my code ? how to replace it ?
hunt
I mean, you don't need to replace `false` with `"false"` in json. your code need to change to able to use `false`, not `"false"`, could you also post some codes using that json?
S.Mark
ohh finally got it !! what u mean ...
hunt
ok, glad to know that :-)
S.Mark