Java: Escape html entity in json string - java

Sorry for my english. My question:
Using jsoup i get json response from site. Some entities have unescaped html charactes, for example
{
"status":200,
"result":[
{
"id":22,
"title":"<p>Мир "Юрского периода" искусств</p>",
"view":1153039,
"image":{
"desktop":{
"image":"img.png",
"svg":"img.svg"
}
},
"part":{
"topic":" Глава\Глава"
}
}
],
"message":""
}
I use Gson. If i try to deserialize this string i will get error on double quotes in title and "\" in topic.
How Can i escape these characters. I only know one way to do this: using regex get string in topic and title and change " to \".
The only problem is that there are many such json entity, and it takes a lot of time. Is there a better way?

Related

Using JsonPath to parse JSON String recursively

I'm trying to parse a given JSON in String format, for example:
{
"id": "indeed",
"interaction_data":
"{\"data\":\"{\\\"something\\\":\\\"blabla\\\"}\",\"somethingElseNotNested\":\"Indeed\"}"
}
I'm working with Kotlin, and I called JsonPath.parse on the value above, the problem is, interaction_data is parsed as a String, instead of it being treated as a JSON as well.
So when I call read("$.interaction_data.data.something") it gives me an error, since interaction_data is treated as a String, instead of an object.
Any way around this? (other than parsing this part separately, I need to handle this generically).
Thanks!
Json interaction_data property is triple stringifyied. Why you don't try this
var jsonObject=..your json;
var jsonParsed=JSON.parse(jsonObject.interaction_data);
jsonParsed.data=JSON.parse(jsonParsed.data);
JsonObject.interaction_data=jsonParsed;
result
{
"id":"indeed",
"interaction_data":{"data"{"something":"blabla"},"somethingElseNotNested":"Indeed"}
}

How do I remove all control characters in a string in Java?

My REST API, which is build with Spring in Java, produces an invalid JSON object, because it contains multiple breaks in a string, which lead to the problem, that the string has an unexpected end and the rest doesn't count as part of the string anymore, example:
{
"status": "Success",
"message": "Lorem ipsum",
"data": {
"correct": [
{
"record": "ULTRA LONG
XML STRING
WITH BREAKS",
"code": 0,
"errors": []
}
]
}
}
The error arises in the data -> correct -> record string field, because it contains breaks which splits the original string.
My API endpoint serializes the above JSON like this:
#PostMapping(value="/check-records",
consumes=MediaType.APPLICATION_JSON_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE)
public Response checkRecords(#RequestBody(required=true) Records records) {
// Check records
return new Response("Success", "Lorem ipsum", data);
}
Response is a class, which automatically gets serialized into a JSON object after returning. data is a map in order to create the above JSON structure.
I couldn't find any suitable solution for my problem yet. Does anybody has an idea how I could remove all breaks, spaces or control characters before I serialize the JSON object?
I appreciate any kind of help, sheers! :)
Thanks to #pringi. He suggested to use a regex to remove all control characters in Java before I serialize the JSON object.
String record = orginalRecord.replaceAll("[\\\\p{Cntrl}^\\r\\n\\t]+", "")
You can find more informations about regex in the original question: How to remove control characters from java string?

org.json.JSONException: Unterminated object at character 103 of

i don't know why this strings contert to Json have error.
{
softName: lovePlay,
packageName: com.feng.play,
softId:13232,
downUrl: http.//mumayi.pay.love/down?id=13211
}
Have somebody can help me? thanks.
Strings should be under double quotes. Something like this:
{
"softName":"lovePlay",
"packageName":"com.feng.play",
"softId":13232,
"downUrl":"http.//mumayi.pay.love/down?id=13211"
}
There are many services on the internet where you be able to validate your json data.
Click here
Your JSON string is incorrect, you're missing the quote signs (").
Try this:
{
"softName": "lovePlay",
"packageName": "com.feng.play",
"softId": "13232",
"downUrl": "http.//mumayi.pay.love/down?id=13211"
}
Thanks all. I have solved this problem. When I use this:
<!-- white space added for readability -->
<input type="button" onclick="getp1('{softName:lovePlay
,packageName:com.feng.play
,softId:13232
,downUrl:\'http\:\/\/mumayi.pay.love\/down\?id=13211\'}')"
value="获取"/>
I can get strings convert to json. I use it by webview.
You have syntax error in your JSON string. You need to change it as below.
{
"softName": "lovePlay",
"packageName": "com.feng.play",
"softId": "13232",
"downUrl": "http.//mumayi.pay.love/down?id=13211"
}
If you are on web dev environment you can use stringify command. Check out this tutorial on JSON and Java
As a good practice, check whether your JSON is valid. There are many tools available but JSONlint is my favourite.
This is my method currently using
public String convertStandardJSONString(String data_json){
data_json = data_json.replace("\\", "");
data_json = data_json.replace("\"{", "{");
data_json = data_json.replace("}\",", "},");
data_json = data_json.replace("}\"", "}");
return data_json;
}

Passing Jackjson JSON object from JSP to JavaScript function

I have a JSON String stored in a database. In one of my JSP pages, I retrieve this string, and I want to be able to pass the String or the JSON object into Javascript function. The function is simply this for test purposes
function test(h){
alert(h);
}
Now I can retrieve the JSON string from the database fine, I have printed it out to the screen to ensure that it is getting it, however when I pass it in like this
<input type="button"
name="setFontButton"
value="Set"
class="form_btn_primary"
onclick="test('<%=theJSON%>'); return false;"/>
Nothing happens. I used firebug to check what was wrong, and it says there is invalid character.
So I then tried passing in the JSON object like so
Widget widg = mapper.readValue(testing.get(0), Widget.class);
Then pass in it
onclick="test('<%=widg%>'); return false;"/>
Now this will pass in without an error, and it alerts the object name, however I am unable to parse it. Object comes in like with the package name of where the widget class is stored like so
com.package.mode.Widget#ba8af9
I tried using Stringify, but that doesn't seem to work on this Jackson JSON object.
After all that failed, I tried a last resort of taking the String from the database, and encoding it in base64. However, this too fails if I do this
String test = Base64.encode(theString);
and pass that in. However if I do that, print it out to the screen, then copy what is printed out, and send that through it works, so don't quite understand why that is.
So could someone please tell me what I am doing wrong. I have tried soo many different solutions and nothing is working.
The JSON String is stored in database like this
{
"id":1,
"splits":[
{
"texts":[
{
"value":"Test",
"locationX":3,
"locationY":-153,
"font":{
"type":"Normal",
"size":"Medium",
"bold":false,
"colour":"5a5a5a",
"italics":false
}
}
]
}
]
}
Would be very grateful if someone could point me in the direct direction!!
Edit:
Incase anyone else has same problem do this to pass the JSON from JSP to the JS function
<%=theJSON.replaceAll("\"", "\\\'")%>
That allows you to pass the JSON in,
then to get it back in JavaScript to normal JSON format
theJSON = theJSON.replace(/'/g,'"');
Should work fine
I think the combination of double quotes wrapping the onclick and the ones in your JSON may be messing you up. Think of it as if you entered the JSON manually -- it would look like this:
onclick="test('{ "id":1, "splits":[ { "texts":[ { "value":"Test", "locationX":3, "locationY":-153, "font":{ "type":"Normal", "size":"Medium", "bold":false, "colour":"5a5a5a", "italics":false } } ] } ] }'); return false;"
and the opening double quote before id would actually be closing the double quote following onclick= (You should be able to verify this by looking at the page source). Try specifying the onclick as:
onclick='test(\'<%=theJSON%>\'); return false;'
You can follow the following steps
Fetch the jon string
Using the jackson or any other JSON jar file , convert the json string to json array and print the string using out.println.
Call this jsp which prints the json string
check in the firebug , you will be able to see your json .
If the Json string does not print , there can be some problems in your json format.
this is a good website for json beautification , http://jsbeautifier.org/ , really makes the string simple to read .
Thanks
Abhi

get attribute value from html code in java

i have HTML string value and i want to get one attribute(id) value from that html String value
can u help me how to do it??
String msHTMLFile = "<ABBR class='HighlightClass' id='highlight40001' style=\"BACKGROUND-COLOR: yellow\" >Fetal/Neonatal Morbidity and Mortality</ABBR>";
result should come - highlight40001;
Try using this regular expression pattern:
\bid='([^']*)'
And then extract the string captured by group 1. This is not foolproof; using regex to parse HTML never is. You can try to complicate the regex to make it more flexible. Or you can just use a HTML parser. I recommend the latter.
Also not so clean, but this should work for you.
You can treat it as xml and parse it using JAXB:
ABBR.java:
import javax.xml.bind.annotation.XmlAttribute;
public class ABBR
{
#XmlAttribute public String id;
}
Main.java:
[..]
String msHTMLFile = "<ABBR class='HighlightClass' id='highlight40001' style=\"BACKGROUND-COLOR: yellow\" >Fetal/Neonatal Morbidity and Mortality</ABBR>";
ABBR obj = JAXB.unmarshal(new StringReader(msHTMLFile), ABBR.class);
System.out.println(obj.id);
[..]
If you're lucky and your HTML source produces XML-compliant HTML, JAXB or other XML parsers will do fine with it. A lot of people aren't writing particularly well-formed HTML (unclosed tags, etc), though some of my coworkers have gotten good results parsing HTML with HotSAX: http://sourceforge.net/projects/hotsax/

Categories

Resources