Sae Us
Sae Us

Reputation: 277

JSON Parsing Syntax - Simple PHP

I have the following json output - I've tried multiple things however not having any luck - I just want to get the value for kind (software-package). Any ideas:

{"items":
[{"assets":
[{"kind":"software-package","url":"__URL__"}],
"metadata":{"bundle-identifier":"SimpleCalculator",
"bundle-version":"000","kind":"software",
"title":"com.work.demo","subtitle":"1.0"}

Thanks,

Upvotes: 0

Views: 46

Answers (3)

Gareth McCumskey
Gareth McCumskey

Reputation: 1540

If you are using a version of PHP above 5.3 then there are built-in functions to decode and encode JSON strings already.

$some_json_string = {......};
$json_string_as_object = json_decode($some_json_string);
$json_string_as_array = json_decode($some_json_string, true);

You can also do the reverse:

$some_array = array(...);
$json_string = json_encode($some_array);

Json Decode : http://php.net/manual/en/function.json-decode.php Json Encode: http://www.php.net/manual/en/function.json-encode.php

Upvotes: 0

Ed Shaw
Ed Shaw

Reputation: 11

That is not valid JSON.

Lets attempts to format that a little more sensibly.

{"items": [
    {"assets": [
        {"kind":"software-package","url":"__URL__"}
     ],
     "metadata":{"bundle-identifier":"SimpleCalculator",
                 "bundle-version":"000","kind":"software",
                 "title":"com.work.demo","subtitle":"1.0"
                }

You're missing a lot of closing brackets. :(

How are you acquiring this JSON. Is it hand formatted or generated by some software? They're may be bugs in the source, and not your code.

Upvotes: 0

GautamD31
GautamD31

Reputation: 28753

Try to decode it with json_encode like

$result_arr = json_decode($my_arr,true);
print_r($result_arr['items']['assets']['kind']);

Upvotes: 2

Related Questions