dongle7637 2010-10-14 00:22
浏览 18
已采纳

PHP JSON数据选择

How would I go about selecting the data of each title from the following JSON? I have the JSON decoded, but I'm not sure how to select the part I want.

{
    "responseData": {
        "results": [
            {
                "title": "Justin Giesbrecht 749",
                "titleNoFormatting": "Justin Giesbrecht 749",
            },
            {
                "title": "Gopher dunes 09",
                "titleNoFormatting": "Gopher dunes 09",
            },
            {
                "title": "dirtbike Justin",
                "titleNoFormatting": "dirtbike Justin",
            },
            {
                "title": "A Warming",
                "titleNoFormatting": "A Warming",
            }
        ],
        "cursor": {
            "pages": [
                {
                    "start": "0",
                    "label": 1
                },
                {
                    "start": "4",
                    "label": 2
                }
            ],
            "estimatedResultCount": "6",
            "currentPageIndex": 0,
        }
    },
    "responseDetails": null,
    "responseStatus": 200
}

I thought it would be something like this, but I don't get anything:

echo "Response ". $jsonS->responseData->results[1]->title;
  • 写回答

4条回答 默认 最新

  • dsxmwin86342 2010-10-14 01:51
    关注

    Actually you've got the reading of the title part right, it's the JSON that is invalid.

    Copying the JSON into a JSON validator/lint e.g. http://www.jsonlint.com/ will show that the you have additional , (commas) after the last object attribute in a few places (5 places to be exact, after each 'titleFormatting' attribute and after 'currentPageIndex').

    If you fix those errors and parse it using json_decode e.g.:

    $jsonS = json_decode($json_text);
    

    Then your own code:

    echo "Response " . $jsonS->responseData->results[1]->title;
    

    Will output the second (index 1 being the second index) results title

    Response Gopher dunes 09

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗