0

Is there an easier way to convert a json string literal to a json object instead of hand coding "\".

Sample data but mine is much larger than this.

{"phonetype":"N95","cat":"WP"}

//Easier way? Equivalent of JSON.Stringify({"phonetype":"N95","cat":"WP"})?
JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}"); 
genxgeek
  • 13,109
  • 38
  • 135
  • 217
  • I don't think there's a way out of this one. Java doesn't support choosing single or double quotes to enclose strings as some other languages like JavaScript. If the JSON is too big to escape quotes, load it from a file. – Ravi K Thapliyal Aug 12 '13 at 16:53
  • 1
    @BrianRoach I think these are similiar but undeniably different questions, the solution to this question may or may not involve stirng escaping techniques. – gbtimmon Aug 12 '13 at 18:42
  • @gbtimmon They're really not. He needs double quotes in a String literal, there's no way around escaping them in Java. What you have posted as an answer is not valid JSON. – Brian Roach Aug 12 '13 at 18:59

3 Answers3

1

What is your end goal? Why does the string need to be in the Java file directly? Why, for that matter, is quote-escaping problematic? To me, it seems like you have an unnecessary constraint (such as "this string needs to be inline" or "this needs to be a string at all") which if you remove will be all around easier to work with.

  1. Store complex JSON outside of Java

    To me this is almost certainly your best bet. You describe your data as being "much larger than [{"phonetype":"N95","cat":"WP"}]" which means it's data, not code, and you'll have a much better time keeping them separate. Load the file up when you need it, using whatever tool you want, and you're golden.

  2. Store your settings as something other than JSON

    JSON is great for many things, but if you need the JSON inline and can't use an external file, that suggests to me that there's some very tight coupling between your Java and your JSON, which doesn't make sense. You would do better to store type-safe constants or otherwise persist this data in "real" Java, rather than in a string which you parse.

  3. Don't worry about the escaping

    I don't think this applies to your case, but if you have a small amount of JSON that you really do want to be inline and JSON, then just drop it in and escape a few quotes and you're done. If this option seems problematic due to a) the data changing regularly or b) the data being large and complex to work with in line that's a pretty clear sign you actually want option 1. above.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Thanks for the reply. The use case was a quick and dirty test (inline within my controller/springmvc) to simulate a json payload to parse. Simple as that. I'm new to java and in lieu of time I didn't want to have to load the json from a file or go in and escape every single relevant token. I was hoping for something like json.stringify(). All points are valid and in this case #1 seems to fit the use case the best. Thx! – genxgeek Aug 12 '13 at 20:54
  • Java in general opts to be more restrictive /verbose where it encourages best practices. While I think a lot of people would agree, multi-line or unescaped string literals would be nice, they were excluded from Java 7 intentionally: http://bugs.sun.com/view_bug.do?bug_id=4165111 A little inconvenient, I'll grant, but taking the extra few moments to compartmentalize your data from your code will often net positive results. – dimo414 Aug 12 '13 at 22:15
0

There are multiple json parsers available to marshal a json to object and vice versa. Few of those parsers are

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 1
    OP is asking how to represent JSON without escaping `"` – sanbhat Aug 12 '13 at 16:48
  • @sanbhat if you marshall a json to object , you don't have to worry about escaping "" – Juned Ahsan Aug 12 '13 at 16:49
  • I'm just looking to simulate a string as a json payload received from a rest call and stuff it into a json object to parse....of course I just want to start from a string represented payload. – genxgeek Aug 12 '13 at 17:07
-1

Mabye this would help?

JSONObject jsonObj = new JSONObject("{'phonetype':'N95','cat':'WP'}"); 
gbtimmon
  • 4,238
  • 1
  • 21
  • 36
  • Except for the part where that's not valid JSON. See: http://stackoverflow.com/questions/2275359/jquery-single-quote-in-json-response – Brian Roach Aug 12 '13 at 19:00
  • (To clarify: because the org.json stuff accepts invalid JSON, this will work ... but it may not in a parser that conforms to the actual spec) – Brian Roach Aug 12 '13 at 19:06