0

如何在PHP中解析其字段名没有双引号的JSON

这是我收到的此类 JSON 的示例....

[{meta_data: [{name: "HANDLE", value: "2"}], compound: { name: "Numeric", entries: [{meta_data: [{name: "partition", value: "2"}, {name: "metric-id", value: "18474"}, {name: "unit-code", value: "2720"}, {name: "unit", value: "bpm"}], simple: {name: "Basic-Nu-Observed-Value", type: "float", value: "72.000000"}}, {compound: { name: "Absolute-Time-Stamp", entries: [{simple: {name: "century", type: "intu8", value: "20"}}, {simple: {name: "year", type: "intu8", value: "12"}}, {simple: {name: "month", type: "intu8", value: "4"}}, {simple: {name: "day", type: "intu8", value: "11"}}, {simple: {name: "hour", type: "intu8", value: "3"}}, {simple: {name: "minute", type: "intu8", value: "10"}}, {simple: {name: "second", type: "intu8", value: "26"}}, {simple: {name: "sec_fractions", type: "intu8", value: "0"}}] }}] }}] 
4

2 回答 2

1

手动,因为没有“”是无效的 JSON。如果要求声明数据将以 JSON 格式交换,那么它不是你的问题,它是应用程序/提供 JSON 的人有问题。推回他们并让他们解决他们的问题。

熊 说:

我正在为他们修理 :) thx

好的,那么您需要修复生成 JSON 的应用程序部分,而不是使用它的部分(假设您json_decode在消费端使用标准)。如果是这种情况,您的初始问题真的变得无关紧要,您可能需要也可能不需要发布一个关于如何最好地重新编写代码以生成正确 JSON 的新问题。

于 2012-04-11T03:24:25.820 回答
0

这是你想要的?我在萤火虫上试过这个。

obj = [{meta_data: [{name: "HANDLE", value: "2"}], compound: { name: "Numeric", entries: [{meta_data: [{name: "partition", value: "2"}, {name: "metric-id", value: "18474"}, {name: "unit-code", value: "2720"}, {name: "unit", value: "bpm"}], simple: {name: "Basic-Nu-Observed-Value", type: "float", value: "72.000000"}}, {compound: { name: "Absolute-Time-Stamp", entries: [{simple: {name: "century", type: "intu8", value: "20"}}, {simple: {name: "year", type: "intu8", value: "12"}}, {simple: {name: "month", type: "intu8", value: "4"}}, {simple: {name: "day", type: "intu8", value: "11"}}, {simple: {name: "hour", type: "intu8", value: "3"}}, {simple: {name: "minute", type: "intu8", value: "10"}}, {simple: {name: "second", type: "intu8", value: "26"}}, {simple: {name: "sec_fractions", type: "intu8", value: "0"}}] }}] }}] 

console.log(obj[0].meta_data);

输出

[Object { name="HANDLE", value="2"}]
于 2012-04-11T03:24:01.077 回答