0

So I have trying to retrieve JSON which contain many text and late display it on the UI using angularJS. For now I encode the text(using java.net.URLEncode) and am getting the results. The JSON responds with the text being converted (sorry I could not frame the question properly) To make more sense of what the problem is I have a similar question posted here

For example

I have a text "My name is""Mike" which is a parameter to a get request which returns a JSON. Now due to the "" (quote or any other special character) the response is an Invalid JSON.(basically he JSON is cut at that point of text)

To solve this I have encoded the text which atleast returns a valid JSON but with the text converted to "My + name + is + " + " + Mike".

text: java.net.URLEncoder.encode(artifact.text, "UTF-8")

This is something I dont want as on the UI i want the original text (without +). I want to do this on the server side.

Right now I have decoded the text on the UI level by

 decodeURIComponent((text+'').replace(/\+/g, '%20'));

but I am assuming the replaced all + in the text but what if + is actually a part of the text eg. "My name is mike+woods". I hope this makes more sense.Json response Single Qoute

Community
  • 1
  • 1
krs8785
  • 1,017
  • 2
  • 13
  • 24
  • 1
    What exactly is the problem? That you have `+` instead of ` `? I don't understand why you are using URLEncode at all. – Jason Goemaat Sep 24 '14 at 20:19
  • 1
    Why do you use urlencode instead of dumping the string as json and then serialise it back from json to string? – DevLounge Sep 24 '14 at 20:19
  • 1
    How do you generate the JSON? `"My+name+is+Mike"` isn't a valid JSON string. Please provide example code. – Bjorn Sep 24 '14 at 20:19
  • `My+name+is+Mike` is still URL-encoded. In URL encoding `+` and `%20` are equivalent. – Jordan Running Sep 24 '14 at 20:23
  • You're trying to create some JSON and you're probably getting an invalid JSON message due to one of the strings in your JSON having characters that need to be escaped. Use some sort of JSON encoding instead of URL encoding as the latter is intended for URL's, see http://stackoverflow.com/questions/3020094/how-should-i-escape-strings-in-json for some help – ckuijjer Sep 24 '14 at 20:24
  • heres the thing. When I dont encode it I get an invalid Json because of quotes or other special cahracters. Thats why without any option I encode it. (unless there is a way to avoid this ). How do i decode thie + thing on the UI ? – krs8785 Sep 24 '14 at 20:25
  • 2
    The JSON library you're using to generate the JSON will correctly escape any values you give it, which will make all of this URL-encoding business unnecessary. Any standard JSON library will never product invalid JSON. It sounds like you might be building strings manually instead of using a JSON library, which is just asking for trouble. – Jordan Running Sep 24 '14 at 20:30
  • @krs can you show example how you encode your object to JSON? – Igor Artamonov Sep 25 '14 at 04:39
  • i have mentioned it above – krs8785 Sep 25 '14 at 12:37
  • can escaping the string help ? – krs8785 Sep 25 '14 at 12:40
  • @IgorArtamonov I am using grails so at the end I do render object as JSON – krs8785 Sep 25 '14 at 12:53
  • @krs `as JSON` escapes everything and produces valid JSON, you don't need to do anything manually – Igor Artamonov Sep 25 '14 at 16:38
  • thats the thing. It should right ? but it does not. It gets trucnated – krs8785 Sep 25 '14 at 16:51
  • http://stackoverflow.com/questions/26041797/escaping-double-quotes-in-string – krs8785 Sep 25 '14 at 16:52
  • http://stackoverflow.com/questions/26039819/escape-string-in-grails-to-avoid-json-error – krs8785 Sep 25 '14 at 16:53

0 Answers0