2

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": [
    {
        "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"       
    } 
 ]
}
Rizon
  • 1,516
  • 4
  • 25
  • 45

3 Answers3

4

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.

svens
  • 11,438
  • 6
  • 36
  • 55
3
  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" 
        } 
    ] 
}
Community
  • 1
  • 1
powtac
  • 40,542
  • 28
  • 115
  • 170
  • my bad. I cut the json and posted just a part of it (1 object) here. and I forgot to trim it well. But this is not the problem though. – Rizon Jan 07 '11 at 12:25
  • So you're saying I need to instruct my Webservice developer to change the way the json is returned? – Rizon Jan 07 '11 at 12:31
  • you mean to change it in the webService right? and if I do so, it will be inconvenient for me to convert it to NSDate object back in Objective-C. Isn't there any other option? – Rizon Jan 07 '11 at 13:03
  • While valid, it's worth noting that this piece of JSON doesn't cause a Date object to show up in the EAt field. Instead it gets the literal string 'new Date(2001,0,7,12,30,0)'. – Dan Ray Jan 07 '11 at 13:27
  • @Rizon: yes, if the string is being returned exactly as shown in your question, it is not valid JSON. End of story. – JeremyP Jan 07 '11 at 13:38
  • 1
    @powtac: Instead of a string representing a Javascript function call to generate a hard coded date, why not actually return a date e.g. `"Eat" : "2011-01-07T12:30:00"` – JeremyP Jan 07 '11 at 13:40
  • @JeremyP: well I think I should instruct him to return the epoch time. e.g. "EAt": "1294408296". That way it will be quite simple to convert to NSDate object – Rizon Jan 07 '11 at 14:03
  • 1
    @Rizon: that will work as long as you both settle on the same epoch base time. It's actually not that hard to convert the date in my example into an NSDate - just use a NSDateFormatter with the format string of `yyyy-MM-dd'T'HH:mm:ss`. For variations on the theme search for `ISO date NSDate` on this web site. – JeremyP Jan 07 '11 at 14:13
  • well, we both gonna settle on 1/1/1970 as this is the convention. Of course your suggestion will work either, so thanks! – Rizon Jan 07 '11 at 14:28
1

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.

Dan Ray
  • 21,623
  • 6
  • 63
  • 87