문제

Could someone point out where the JSON error is in the following...

"{\"a\"=>\"b\"}"

I get the follow error when doing JSON.parse

JSON::ParserError: 757: unexpected token at '{"a"=>"b"}'

Thanks

UPDATE

Yep, as the commenter points out, I can't parse it bc it is a Ruby hash literal. FWIW I was dealing with this value when trying to do nested hashes inside of PG Hstore. Turns out, you shouldn't really do that / that is not what Hstore is currently designed to support. If you wanted to stick with this approach you can do the following to get the hash value:

eval("{\"a\"=>\"b\"}")
도움이 되었습니까?

해결책

That's not JSON. If I assume the outer quotes and the escapes on the inner quotes are not really in the data, that's:

{"a"=>"b"}

JSON doesn't use =>. In JSON it would be

{"a":"b"}

Edit: Ah, @falsetru says in a comment that what you have there is a Ruby Hash literal. I'm not a Ruby guy.

다른 팁

Here is a simple answer to convert a PG Hstore string (data) into a javascript object :

JSON.parse( data.replace(/=>/g,':') )

or

JSON.parse( data.replace(/"/g,'"').replace(/=>/g,':') )
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top