访问json对象中的数字属性 [英] Accessing numerical property in a json object

查看:238
本文介绍了访问json对象中的数字属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP多维数组,我使用JSON_encode()转换为JSON。

I have a PHP multidimensional array witch i converted to JSON using JSON_encode().

我正在使用drupal所以对于那些不熟悉它的人来说,drupal数组经常有看起来像这样的键:

I am using drupal so for those not familiar with it, drupal array often have keys that looks like that :

$some_array['und']['0']['value']

所以我的JSON对象最终看起来像:

So my JSON object ends up looking like:

some_array.und.0.value

现在的问题是当使用上面的语法来检索值时,FB控制台中出现以下JS错误:。运算符后缺少名称

Now the problem is that when use the above syntaxe to retreive the value i the following JS error in the FB console : "missing name after . operator"

此外数据意味着与Jquery模板一起使用,所以我尝试直接在我的模板中访问这些数据:

Also this data is meant to be used with a Jquery template, so i alos tried accessing this data directly in my template with:

${field_city.und.0.value}

不幸的是,巫婆也没有工作..

Witch unfortunately didn't work either..

我如何解决这个问题?我能以某种方式访问​​这个与JS的关系吗?或者有一种方法,我有JSON_encode函数替换所有0让我们说零?或者甚至在解析JSON字符串时替换'0'?

How would i go to fix that issue ? Can i access somehow this proprety with JS? Or is there a way that i have JSON_encode function replace all 0 by let's say "zero"? Or even replacing '0' when parsing the JSON string?

建议很多。

推荐答案

除了使用点表示法之外,您还可以使用常规数组表示法来访问JSON节点:

As well as using the dot notation, you can use regular array notation to access JSON nodes:

some_array.und['0'].value

这篇关于访问json对象中的数字属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆