1

From server I get json. Json is very big. I show litle piece of this

{
      "id": "9429531978965160",
      "name": "Morning in  "Paris"",  // json.net cannot deserialize this line, because line have no escaped quotes.
     "alias": "ThisAlias"
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1088259
  • 345
  • 13
  • 34
  • 3
    If you're receiving that text as quoted, you're not receiving JSON. You're receiving a bunch of characters that is vaguely JSON-like, but which is not valid (and cannot be reasonably be parsed). The problem needs to be fixed at the source, which is whatever is generating that mess. The `name` property should be output like this: `"name": "Morning in \"Paris\"",` More: http://json.org – T.J. Crowder Mar 29 '13 at 12:03
  • 4
    Looks like you need to get the server to generate proper json – hd1 Mar 29 '13 at 12:03
  • 1
    invalid JSON from server, more info: http://stackoverflow.com/a/2275428/169714 – JP Hellemons Mar 29 '13 at 12:09
  • your are right. Server get application/javascript. And how it convert to json??? – user1088259 Mar 29 '13 at 12:21

2 Answers2

1

It's not just that the output you are receiving is non-standard json, it's broken in such a way that it's not a well-defined language and doesn't parse unambiguously even in the simple cases. How should you parse {"a": "A", "b": "B"}? One way is as legal json. Another valid parse is a single property a with the value "A\", \"b\": \"B".

As others have said, the best resolution is to fix the server so that it no longer outputs invalid garbage. If that's not an option, you'll have to write your own parser. A normal parser would declare an syntax error at the 'P' in "Paris". Your parser could back up to the last quote token and try to treat it as if it were escaped. The next syntax error is at the second of the consecutive quotes, and again it could back up and treat the quote token as if it were escaped. If there are any other ways in which the input deviates from legal json you'll need to handle those as well.

If you're not familiar with parsers, this will take a while. And when you're done you'll have a parser that recognizes a poorly-specified and almost totally useless language, which is to say that it will largely be a waste of time. Do what you can to fix it on the server side.

bmm6o
  • 6,187
  • 3
  • 28
  • 55
0

The problem is the server side that generates invalid JSON.

You could try writing a regex that fixes this (searches for any quotes in between the third and last). Just note that there might be many other issues with the JSON, like newlines that are not escaped etc.

Knaģis
  • 20,827
  • 7
  • 66
  • 80