Home > Backend Development > PHP Tutorial > javascript - 三级json数据去掉属性名的引号

javascript - 三级json数据去掉属性名的引号

WBOY
Release: 2016-06-06 20:09:11
Original
1234 people have browsed it

php 从数据库中获取数据 处理之后 $this->ajaxReturn($data);
得到以下数据,但js端要的是 name和sub 去掉引号的数据,{name: "北京市"}
在js端 typeof 类型是 object
在Php处理方便还是js处理方便,
分别是怎么处理的。

<code>[
    {
      "name": "北京市",
      "sub": [
        {
          "name": "东城区"
        },
        {
          "name": "西城区"
        },
        {
          "name": "朝阳区"
        },
        {
          "name": "海淀区"
        },
        {
          "name": "丰台区"
        },
        {
          "name": "石景山区"
        },
        {
          "name": "顺义区"
        },
        {
          "name": "昌平区"
        },
        {
          "name": "门头沟区"
        },
        {
          "name": "通州区"
        },
        {
          "name": "房山区"
        },
        {
          "name": "大兴区"
        },
        {
          "name": "延庆区"
        },
        {
          "name": "怀柔区"
        },
        {
          "name": "平谷区"
        },
        {
          "name": "密云区"
        },
        {
          "name": "亦庄开发区"
        }
      ]
    }
  ]</code>
Copy after login
Copy after login

回复内容:

php 从数据库中获取数据 处理之后 $this->ajaxReturn($data);
得到以下数据,但js端要的是 name和sub 去掉引号的数据,{name: "北京市"}
在js端 typeof 类型是 object
在Php处理方便还是js处理方便,
分别是怎么处理的。

<code>[
    {
      "name": "北京市",
      "sub": [
        {
          "name": "东城区"
        },
        {
          "name": "西城区"
        },
        {
          "name": "朝阳区"
        },
        {
          "name": "海淀区"
        },
        {
          "name": "丰台区"
        },
        {
          "name": "石景山区"
        },
        {
          "name": "顺义区"
        },
        {
          "name": "昌平区"
        },
        {
          "name": "门头沟区"
        },
        {
          "name": "通州区"
        },
        {
          "name": "房山区"
        },
        {
          "name": "大兴区"
        },
        {
          "name": "延庆区"
        },
        {
          "name": "怀柔区"
        },
        {
          "name": "平谷区"
        },
        {
          "name": "密云区"
        },
        {
          "name": "亦庄开发区"
        }
      ]
    }
  ]</code>
Copy after login
Copy after login

php返回json数据字符串,js将返回的json字符串解析为json对象就可以

ajax请求设置dataType:'josn'
php返回的时候json_encode处理下
前端接收到的数据就是object了

有没有引号并不影响它作为对象的属性。

将json转换为js对象再去操作,JSON.parseJSON(data)


data=JSON.parse(data);
jQuery.each(data, function(k,v) {});
并不会取到“”?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template