在PHP中解析JSON字符串以显示其内容

Im trying to use the json_decode but i think the [] at either end of result has something to do with it.

i need to access some parts of this JSON.

In PHP I do:

<?php
  $arr = json_decode($json, true);
  print $arr['ua']['rawUa']; 
?>

The actual JSON (from here) is:

[
  {
    "meta": {
      "name": "ua-parser",
      "repo": "https://github.com/tobie/ua-parser",
      "version": "0.3.5"
    },
    "ua": {
      "rawUa": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2718.0 Safari/537.36",
      "string": {
        "family": "Chrome",
        "major": "52",
        "minor": "0",
        "patch": "2718"
      },
      "family": "Chrome",
      "major": 52,
      "minor": 0,
      "patch": 2718,
      "device": {
      "family": "Other"
    }
  },
  "os": {
    "string": {
      "family": "Mac OS X",
      "major": "10",
      "minor": "11",
      "patch": "1",
      "patchMinor": null
    },
  "family": "Mac OS X",
  "major": "10",
  "minor": "11",
  "patch": "1"
  }
},
{
  "meta": {
    "name": "ua-parser-js",
    "repo": "https://github.com/faisalman/ua-parser-js",
    "version": ""
  }
},
{
  "meta": {
    "name": "platform.js",
    "repo": "https://github.com/bestiejs/platform.js/",
    "version": "1.3.0"
    },
    "ua": {
      "name": "Chrome",
      "version": "52.0.2718.0",
      "layout": "Blink"
    },
    "os": {
      "os": {
        "architecture": 32,
        "family": "OS X",
        "version": "10.11.1"
      }
    },
    "device": {
      "product": null,
      "manufacturer": null,
      "description": "Chrome 52.0.2718.0 on OS X 10.11.1"
    }
  }
]

try using :

print $arr[0]['ua']['rawUa'];

Just use the first index as 0. your array first index start with 0.

$arr = json_decode($json, true);

echo $arr[0]['ua']['rawUa'];

try this you have to get the data from index zero, to check that try print the array var_dump($arr);

$arr = json_decode($data, true);

print $arr[0]['ua']['rawUa']; 

Use foreach loop to fetch all records.

$arr = json_decode($json, true);

foreach($arr as $row)
{
    if(isset($row['ua']['rawUa'])
    {
         echo $row['ua']['rawUa'];
    }
}

For single Record Use array index [0]

print $arr[0]['ua']['rawUa']; 

var_dump($arr);

Result part:

array (size=3)
  0 => 
    array (size=3)
      'meta' => 
        array (size=3)
          'name' => string 'ua-parser' (length=9)
          'repo' => string 'https://github.com/tobie/ua-parser' (length=34)
          'version' => string '0.3.5' (length=5)
      'ua' => 
        array (size=7)
          'rawUa' => string 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2718.0 Safari/537.36' (length=119)