jQuery.parseJSON无法解析有效的json

时间:2011-10-05 14:26:47

标签: php jquery json

load    : function() {
    var mastery = $('div.mastery.trees');
    this.object = $.parseJSON($('a.json.data', mastery).text());
    console.log(this.object);
}

这无声地失败(除非我在parseJSON周围添加一个try / catch块),无论json数据的内容是什么(我想使用的数据 - 见下文 - 或者甚至只是一些非常简单的测试数据)

实际数据

{"points":{"allowed":30,"current":0,"offense":0,"defense":0,"utility":0},"current":{"offense":{},"defense":{},"utility":{}},"trees":{"offense":{"Deadliness":{"max":3,"req":""},"Cripple":{"max":1,"req":""},"Plentiful_Bounty":{"max":1,"req":""},"Archmages_Savvy":{"max":3,"req":""},"Sorcery":{"max":4,"req":{"offense":4}},"Alacrity":{"max":4,"req":{"offense":4}},"Burning_Embers":{"max":1,"req":{"offense":8}},"Archaic_Knowledge":{"max":1,"req":{"offense":8,"Sorcery":4}},"Sunder":{"max":3,"req":{"offense":8}},"Offensive_Mastery":{"max":2,"req":{"offense":8}},"Brute_Force":{"max":3,"req":{"offense":12}},"Lethality":{"max":3,"req":{"offense":16}},"Improved_Rally":{"max":1,"req":{"offense":16}},"Havoc":{"max":1,"req":{"offense":20}}},"defense":{"Menders_Faith":{"max":1,"req":""},"Resistance":{"max":3,"req":""},"Preservation":{"max":1,"req":""},"Hardiness":{"max":3,"req":""},"Strength_of_Spirit":{"max":3,"req":{"defense":4,"Resistance":3}},"Evasion":{"max":4,"req":{"defense":4}},"Defensive_Mastery":{"max":2,"req":{"defense":8}},"Nimbleness":{"max":1,"req":{"defense":8,"Evasion":4}},"Harden_Skin":{"max":3,"req":{"defense":8,"Hardiness":3}},"Veterans_Scars":{"max":4,"req":{"defense":12}},"Willpower":{"max":1,"req":{"defense":12}},"Ardor":{"max":3,"req":{"defense":16}},"Reinforce":{"max":1,"req":{"defense":16}},"Tenacity":{"max":1,"req":{"defense":20}}},"utility":{"Spatial_Accuracy":{"max":1,"req":""},"Good_Hands":{"max":3,"req":""},"Perseverance":{"max":3,"req":""},"Haste":{"max":1,"req":""},"Awareness":{"max":4,"req":{"utility":4}},"Expanded_Mind":{"max":4,"req":{"utility":4}},"Greed":{"max":1,"req":{"utility":8}},"Meditation":{"max":3,"req":{"utility":8}},"Utility_Mastery":{"max":2,"req":{"utility":8}},"Insight":{"max":1,"req":{"utility":8}},"Quickness":{"max":3,"req":{"utility":12}},"Blink_of_an_Eye":{"max":1,"req":{"utility":12}},"Intelligence":{"max":3,"req":{"utility":16}},"Mystical_Vision":{"max":1,"req":{"utility":16}},"Presence_of_the_Master":{"max":1,"req":{"utility":20}}}}}

测试数据

{"json":1}

jsonlint.com告诉我这两个数据集都是有效的json所以为什么它不能正确解析?

所有数据都来自通过json_encode()

转换的php数组
return array(
    "points"    => array( // for js
        "allowed"   => 30,
        "current"   => 0,
        "offense"   => 0,
        "defense"   => 0,
        "utility"   => 0,
    ),
    "current"   => array( // for js
        "offense"   => new stdClass,
        "defense"   => new stdClass,
        "utility"   => new stdClass,
    ),
    "trees"     => array(
        "offense"   => $offense, // the trees
        "defense"   => $defense,
        "utility"   => $utility,
    ),
);

盯着它看了一会儿,只是看不出错误来自哪里

有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

从你的问题:

  

一个简单的错误被忽略了,我只是在接受时   按照JamWaffles的建议检查console.log

     

它的要点就是这个; var mastery正在拿起2个div(父母   我想要的和直接的孩子,我不这样做 - 和两者都是   父母对json数据..那就是重复来自

     

     

感谢jamwaffles!