简体   繁体   English

json在php上获取正确的数据

[英]json getting the right data on php

How do I get the output of Apple to be fruit and Beef to be meat with this kind of JSON? 如何使用这种JSON将Apple的输出结果转换为水果,将Beef的输出转换为肉? Is it even possible to get that data? 是否有可能获得该数据?

PS each array's first index will always be 1. PS每个数组的第一个索引将始终为1。

Example of desired output: 所需输出示例:

  1. Apple -> fruit 苹果->水果
  2. Beef -> meat 牛肉->肉
  3. Celery -> vegetable 芹菜->蔬菜
{
    "Apple": {
        "fruit": "1"
    },
    "Beef": {
        "meat" : "1"
    },
    "Celery": {
        "vegetable" : "1"
    }
}

You can use json_decode : 您可以使用json_decode

$obj = json_decode( $jsonString );
echo $obj->Apple->fruit;

et voila! 等等!

PS: Your above JSON is unvalid (maybe a typo): please add a : after "Celery" PS:您上面的JSON无效(可能是拼写错误):请在"Celery"之后添加一个:

PS2: each array's first index is not 1 , index(es) are 'Apple', 'Beef' and 'Celery'. PS2:每个数组的第一个索引都不为1 ,索引为“ Apple”,“ Beef”和“ Celery”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM