2011-03-06 33 views
1

以下JSON数据故障解析上的JSON数据

'{ 
    {"errors": 
    {"firstname":"is too short"} 
    }, 
    {"account": 
    {"firstname":"Test"} 
    } 
}' 

对于其中得到这个错误有什么不对的解析?

JSON::ParserError in AccountsController#home 
706: unexpected token at ... # the code above 

在AccountsController我有

JSON.parse(json_data)["errors"] 

回答

3

你缺少属性名称:

'{"property1": 
    {"errors": 
    {"firstname":"is too short"} 
    }, 
    "property2": 
    {"account": 
    {"firstname":"Test"} 
    } 
}' 

或者,你真的想要一个数组:

'[ 
    {"errors": 
    {"firstname":"is too short"} 
    }, 
    {"account": 
    {"firstname":"Test"} 
    } 
]' 
4

你不应该封装属性的错误和账户。它应该看起来像这样:

'{ 
    "errors":{"firstname":"is too short"}, 
    "account":{"firstname":"Test"} 
}' 
+0

这是给出数据的最佳场景。 – Satya 2011-03-06 21:30:07

+0

+1我同意Satya。 – awm 2011-03-07 04:34:39