如何用php解析这个json? [英] How to parse this json with php?

查看:114
本文介绍了如何用php解析这个json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的json文件:

I got a json file like this:

[{
    city: "myCity",
    lat: 11.1111,
    format: "etc",
    lng: 1.11,
    street: "mystreet",
    hours: [{
        A: "0230",
        B: "2200",
        C: "16-04-2012"
    }, {
        A: "0130",
        B: "2200",
        C: "11-04-2012"
    }, {
        A: "0230",
        B: "2200",
        C: "18-04-2012"
    }, {
        A: "0130",
        B: "2200",
        C: "12-04-2012"
    }, {
        A: "0230",
        B: "2200",
        C: "20-04-2012"
    }, {
        A: "0130",
        B: "2200",
        C: "13-04-2012"
    }, {
        A: "0230",
        B: "2200",
        C: "22-04-2012"
    }, {
        A: "0130",
        B: "2200",
        C: "14-04-2012"
    }, {
        A: "0230",
        B: "2200",
        C: "24-04-2012"
    }, {
        A: "0130",
        B: "2200",
        C: "15-04-2012"
    }, {
        A: "0230",
        B: "2200",
        C: "26-04-2012"
    }, {
        A: "0130",
        B: "2200",
        C: "16-04-2012"
    }, {
        A: "0230",
        B: "2200",
        C: "28-04-2012"
    }, {
        A: "0130",
        B: "2200",
        C: "17-04-2012"
    }],
    housenumber: "12"
}]

Firebug可以毫无问题地进行解析.它说小时"是一个其中包含某些对象的对象,其中包含F,U和D.但是我无法使用常规的php解析器对其进行解析.我几乎要将其更改为XML,但这将花费我很多时间,并且如果Firebug可以处理这种格式,则必须有一些解析器可以处理这种格式!

Firebug parses this without a problem. It says "hours" is an object that has certain objects in it that contains a F, U and D. But I cannot get parse it with the regular php parsers. I was almost changing it to XML, but that would cost me a lot of time and there must be some parser that can handle this format if firebug can handle it!

推荐答案

我确定这是重复项,但找不到.而且由于注释被忽略,因此我将其再次添加为答案.

I'm certain this is a duplicate, but can't find it. And because comments go ignored, I'll just add it as answer again.

如果您需要解码Javascript表达式(它没有JSON所要求的引号),则可以使用:

If you need to decode a Javascript expression (it doesn't have key quotes as JSON would require it) you can use:

  • http://pear.php.net/package/Services_JSON
    which has some fallback logic to process unquoted keys

http://include-once.org/p/upgradephp/#upgrade.php.prefixed
up_json_decode($json, FALSE, 512, JSON_PARSE_JAVASCRIPT)

作为不建议的替代方法,可以使用一些正则表达式和字符串修补变通办法来预处理未加引号的键.

As inadvisable alternative there are some regex and string patching workarounds for pre-processing unquoted keys.

这篇关于如何用php解析这个json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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