dongshengheng1013 2013-11-25 15:34
浏览 24
已采纳

在json解码不能正常工作

I want to get key and values from a json string like below

JSON :

[{
    "EXTAPP_ID": "9901",
    "CATEGORY_ID": "10",
    "LANGUAGE_CODE": "tr",
    "CATEGORY_LANG_DESC": "Sat\u0131\u015f Departman\u0131"
}, {
    "EXTAPP_ID": "9901",
    "CATEGORY_ID": "10",
    "LANGUAGE_CODE": "de",
    "CATEGORY_LANG_DESC": "Sales Abteilung"
}, {
    "EXTAPP_ID": "9901",
    "CATEGORY_ID": "20",
    "LANGUAGE_CODE": "de",
    "CATEGORY_LANG_DESC": "Procurement Abteilung"
}, {
    "EXTAPP_ID": "9901",
    "CATEGORY_ID": "20",
    "LANGUAGE_CODE": "tr",
    "CATEGORY_LANG_DESC": "Sat\u0131nalma Departman\u0131"
}, {
    "EXTAPP_ID": "9901",
    "CATEGORY_ID": "10",
    "LANGUAGE_CODE": "en",
    "CATEGORY_LANG_DESC": "Sales Department"
}, {
    "EXTAPP_ID": "9901",
    "CATEGORY_ID": "20",
    "LANGUAGE_CODE": "en",
    "CATEGORY_LANG_DESC": "Procurement Department"
}]

PHP :

$string = json_decode($_POST['json'], true);
foreach($string as $key => $value) {
  echo $key . " : " . $value;
}

it returns

0 : Array1 : Array2 : Array3 : Array4 : Array5 : Array
  • 写回答

2条回答 默认 最新

  • dongtuanzi1080 2013-11-25 15:37
    关注

    You are getting that output because your JSON has an array of objects (which PHP is parsing as an array of arrays). You need a nested loop:

    foreach($string AS $array) {
        foreach($array AS $key => $value) {
            echo $key . " : " . $value;
        }
    }
    

    Working example: http://3v4l.org/Ee1iF

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

报告相同问题?

悬赏问题

  • ¥15 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧