开发者

JSON validation

I'm trying to work with json-framework on iPhone to parse a json string. When I'm calling this method:

NSDictionary *dictionary = [jsonString JSONValue];

I'm getting the error:

"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key:
 Options\" UserInfo=0x4b5f390 {NSUnderlyingError=0x4b5f320 \"Expected value while
 parsing array\", NSLocalizedDescription=Object value expected for key: Options}"

According to this json validator [1]: http://www.jsonlint.com// my json is not valid. But is that so??

My json string looks like this:

{
"Options":开发者_StackOverflow中文版 [
    {
        "ID": "7",
        "A": "1",
        "EAt": new Date(2011,
        0,
        7,
        12,
        30,
        0),
        "Type": "Binary",       
    } 
}

* Edited Json: (still brings up an error)

{
"Options": [
    {
        "ID": "7",
        "A": "1",
        "EAt": new Date(2011,
        0,
        7,
        12,
        30,
        0),
        "Type": "Binary"       
    } 
 ]
}


Your JSON is not valid.

It's because you can't create object instances within JSON. It's not a valid value.

new Date(2011, 0, 7, 12, 30, 0)

And you missed the closing array bracket. Everything else is ok.


  1. remove the comma after ...Binary"
  2. add a ] between the two } }.
  3. Date cant be used like this, see How do I format a Microsoft JSON date? and http://msdn.microsoft.com/en-us/library/bb299886.aspx#intro_to_json_sidebarb

This is valid:

{
    "Options": [
        {
            "ID": "7",
            "A": "1",
            "EAt": "new Date(2011,0,7,12,30,0)",
            "Type": "Binary" 
        } 
    ] 
}


You can't instantiate Date objects (or any objects) in a JSON string.

You need to have whoever's responsible for the code that emits this JSON change it to emit valid JSON. They're putting out something now that can't work with any JSON parser. Maybe they have a customized JSON consumer that can handle such things, but this isn't standard JSON.

If I were you, I'd have them put the string of the current date into that field (so: "2011-07-01 12:30:00") and then parse that in your obj-cusing NSDateFormatter.

If whatever puts out that JSON isn't something you can change, you can always modify it locally before feeding it to the JSON library. It's just a string, nothing magical.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜