Java - '\' not being used to escape double quotes " in string - java

I have some JSON content in bytebuffer as : {\"ID\":101}"}
This content is being returned from a service invocation. I get result from that microservice in a bytebuffer. (This means - I cannot get the content changed)
Now, I need to get the json object from this buffer. I'm using this code :
ByteBuffer payloadByteBuffer = invokeResult.getPayload();
byte[] payloadByteArray = payloadByteBuffer.array();
rawJson = new String(payloadByteArray, Charset.forName("UTF-8"));
System.out.println("Raw JSon result = "+rawJson);
The string that gets printed is : "{\"ID\":101}"
Please note that '\' is getting printed within the string but it is originally used to escape double quote. So, when I try to convert this string to JSON object, I get an error :
"Missing value at 1 [character 2 line 1]"
which is probably due to '\' not being used to escape double quote character(I think).
So, My question is, how do I modify my string to treat '\' character for it's correct purpose?
I have tried replacing "\". but it didn't work. I don't know why.
I have also tried different charset encoding : US-ASCII and ASCII but got the same result.

You can try json-simple. You can use the dependency from here
P.S: Your JSON response is wrong.
ByteBuffer payloadByteBuffer = invokeResult.getPayload();
byte[] payloadByteArray = payloadByteBuffer.array();
rawJson = new String(payloadByteArray, Charset.forName("UTF-8"));
JSONParser parser = new JSONParser();
try {
JSONObject json = (JSONObject) parser.parse(rawJson);
} catch (ParseException e) {
e.printStackTrace();
}
It parses the JSON with \ in it.

How about using the apache's commons lang library?
I think it's a simple and easy way of remove your problem away.
Here is my full test code for you.
package just.test;
import java.nio.charset.Charset;
import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONObject;
import com.ibm.icu.impl.ByteBuffer;
public class UnescapeCharTest {
private static void testJSONString(final String rawJson)
{
JSONObject json = null;
try
{
json = new JSONObject(rawJson);
System.out.println("ID = "+json.get("ID"));
}
catch(org.json.JSONException je)
{
je.printStackTrace();
}
String convJson = rawJson.replace("\\", "");
try
{
json = new JSONObject(convJson);
System.out.println("ID = "+json.get("ID"));
}
catch(org.json.JSONException je)
{
je.printStackTrace();
}
convJson = StringEscapeUtils.unescapeJson(rawJson);
try
{
json = new JSONObject(convJson);
System.out.println("ID = "+json.get("ID"));
}
catch(org.json.JSONException je)
{
je.printStackTrace();
}
}
public static void main(String[] args)
{
String rawJson = "{\\\"ID\\\":101}";
testJSONString(rawJson);
String rawJson2 = null;
ByteBuffer payloadByteBuffer = ByteBuffer.wrap(rawJson.getBytes());
byte[] payloadByteArray = payloadByteBuffer.array();
rawJson2 = new String(payloadByteArray, Charset.forName("UTF-8"));
testJSONString(rawJson2);
}
}
I hope the code is right for you.
Have a good coding...

Related

How to return JSON response from a URL returning HTML

First, some background :-
I'm trying to solve a question asked by an interviewer recently. I had to write a code and use below URL to return JSON response -
https://losangeles.craigslist.org/
This is what I did :-
1) I created a webclient and made HTTPURL Request to fetch an HTTP Response.
public static JSONArray getSearchResults(String arg) {
JSONArray jsonArray = null;
try {
QueryString qs = new QueryString("query", arg);
URL url = new URL("https://toronto.craigslist.ca/search?"+qs);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/text");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String readAPIResponse = " ";
StringBuilder output = new StringBuilder();
while ((readAPIResponse = br.readLine()) != null) {
output.append(readAPIResponse);
}
jsonArray = convertToJson(output);
System.out.println(" JSON response : "+jsonArray.toString(2));
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonArray;
}
2) Below was my function to convert the response into JSON :-
public static JSONArray convertToJson(StringBuilder response) {
JSONArray jsonArr = new JSONArray();
if (response != null) {
try {
Document document = Jsoup.parse(response.toString());
Elements resultRows = document.getElementsByClass("result-row");
JSONObject jsonObj;
for (int i = 0; i < resultRows.size(); i++) {
jsonObj = new JSONObject();
Element e = resultRows.get(i);
Elements resultsDate = e.getElementsByClass("result-date");
Elements resultsTitle = e.getElementsByClass("result-title hdrlnk");
String key1 = "date";
String value1 = resultsDate.get(0).text();
jsonObj.put(key1, value1);
String key2 = "title";
String value2 = resultsTitle.get(0).text();
jsonObj.put(key2, value2);
jsonArr.put(i, jsonObj);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonArr;
}
The response I received was the whole HTML page(I used postman to make requests). Since, I only had few hours to solve this question and was not sure how to parse an entire HTML, I ended up using a third party library, called JSoup. I was not 100% happy about it, but ended up having no other option.
I have not heard back from them and I am curious if this was the worst approach and if yes, what could be better options? They did not mention anything about what technology I could use. But,since the skill set I was interviewing involved Java/J2EE I was thinking to implement this in Java (Not using Node js though)
Thanks!
If you only need an XML Parser which is obviously the base of HTML this is built in in the JRE core API.
Even in the SE Version the needed packages to parse exist:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
Take a look at these classes they are the most important to parse or create an XML/HTML File
DocumentBuilderFactory
DocumentBuilder
Document
and here simple example for HTML
String text = "<html><head>HEAD</head><body>BODY</body>";
ByteArrayInputStream input = new ByteArrayInputStream(text.getBytes("UTF-8"));
Document doc = builder.parse(input);

Java Eclipse - 'Unexpected token END OF FILE at position 0' when trying to read from JSON

I am using Java Eclipse to create an event management system which will write and read JSON files. Here I have code which creates a new JSON file...
public void actionPerformed(ActionEvent arg0) {
//Declaration of variables
String title = txtTitle.getText();
String month = (String) cboMonth.getSelectedItem();
String day = (String) cboDate.getSelectedItem();
String year = (String) cboYear.getSelectedItem();
String location = txtLocation.getText();
String description = txtDescription.getText();
String URL = txtURL.getText();
// Combine multiple variables together to make a single variable
String date = month + "" + day + "" + year;
// Create a new instance of the class called 'Event'
Event event = new Event();
// Assign values to the getter/setter methods of this instance
event.setName(title);
event.setDate(date);
event.setLocation(location);
event.setDesc(description);
event.setURL(URL);
// Add this new instance to the 'eventList' array list
MainMenu.eventList.add(event);
// Create a new instance of the class called 'Event'
JSONObject JSONEvent = new JSONObject();
// Add data to the JSON file
JSONEvent.put("Title", title);
JSONEvent.put("Date", date);
JSONEvent.put("Location", location);
JSONEvent.put("Description", description);
JSONEvent.put("URL", URL);
// Create a new JSON file called 'Events.json' that has elements added to it
try (FileWriter file = new FileWriter("Events.json", true)) {
file.write("\n");
file.write(JSONEvent.toJSONString());
file.flush();
// Error Handling
} catch (IOException e) {
e.printStackTrace();
}
}
This code works perfectly fine by creating a JSON file and populating it with JSONObjects. Here is an image of what a single entry in the JSON file looks like... Single JSON Element
I then have a separate class with the following code which attempts to read the JSON file and output its contents to the console...
public static void main(String[] args) {
JSONObject JSONEvent;
String line = null;
try {
FileReader fileReader = new FileReader("Events.json");
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
JSONEvent = (JSONObject) new JSONParser().parse(line);
String title = (String) JSONEvent.get("Title");
System.out.println(title);
String date = (String) JSONEvent.get("Date");
System.out.println(date);
String location = (String) JSONEvent.get("Location");
System.out.println(location);
String description = (String) JSONEvent.get("Description");
System.out.println(description);
String URL = (String) JSONEvent.get("URL");
System.out.println(URL);
Event event = new Event();
event.setName(title);
event.setDate(date);
event.setLocation(location);
event.setDesc(description);
event.setURL(URL);
MainMenu.eventList.add(event);
}
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
When I run this code, I get the following errors in the console...
Unexpected token END OF FILE at position 0
Does anyone have any idea what this error means?
The very first thing you write to the file is an empty line:
file.write("\n");
So, when reading the file, you're trying to parse an empty string to JSON, hence the exception: the parser finds the end of its input before even having a chance to parse anything.
Instead of relying on the internal format of the generated JSON, and to write several distinct JSON objects to the file, it would be simpler, and safer, to write a single array of objects all at once in the file (replacing its previous content), and to read the whole array at once in memory.
Faced a similar issue. Was trying to write a JSON data record from a file on a Kafka topic. Was getting Unexpected token END OF FILE at position . As a fix, I cleared all the \n characters from the file and tried sending it to the kafka topic. It worked.

JUnit test shows string not equal when should be equal

So, I'm currently doing a JUnit test for saving and loading a save file in JSON format. However, while saving works without a problem, I keep running to an issue where a string I read in from the file is not kept in the variable I'm writing it to.
public void loadSettings(Vector<User> U, String url, String filepath) {
JSONParser parser=new JSONParser();
try {
Object obj=parser.parse(new FileReader(filepath));
JSONObject jsonObject=(JSONObject) obj;
url=jsonObject.get("URL").toString();
//System.out.println(URL);
JSONArray users=(JSONArray)jsonObject.get("Users");
Iterator<Object> iter=users.iterator();
while(iter.hasNext()) {
String temp1=iter.next().toString();
Long temp2=(Long)iter.next();
User n=new User(temp1,temp2.intValue());
U.addElement(n);
}
} catch (Exception e) {
e.printStackTrace();
}
}
The string in question is the "url" string. The values for the User class I have are inserted in fine. I don't know what the problem is, and I've been working on this for hours. Can anyone help?
URL may contains some special characters, you can try like this:
URLEncoder.encode("This string has spaces", "UTF-8");
then store in the file.
last do this:
url = URLDecoder.decode(jsonObject.get("URL").toString(), "UTF-8");

How to read a JSON file from Assests folder instead of URL in Volley

I have a JSON file in the assets folder. I'd like to read this file and do something with the data. How can I use Volley for this? I can't find anything like this. I don't want to use more libraries like gson or jackson.
Can I handle it with just Volley?
Thanks a Lot.
You dont need volley to read a Json file from the asset directory.
In my case, I load an array of Json from the file into my string "filePath".
final String filePath = readFromAsset(act, "json_to_load.json"); //act is my current activity
try {
JSONArray obj = new JSONArray(filePath);
for (int i = 0; i < obj.length(); i++) {
JSONObject jo = obj.getJSONObject(i);
// do stuff
}
} catch (JSONException e) {
e.printStackTrace();
}
In my utils file :
private static String readFromAsset(Activity act, String fileName)
{
String text = "";
try {
InputStream is = act.getAssets().open(fileName);
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
text = new String(buffer);
} catch (IOException e) {
e.printStackTrace();
}
return text;
}
To be able to use it your have to import the package "org.json;".
Hope it helps !
Volley itself is not able to parse JSON, so you need to use GSON or ...

JAVA send/receive information via json

I've been researching on how to send and receive information to a url, via json for the last 3 days. I have found a lot of documentation and code examples on how to do it, I just can't comprehend what they're saying. I've imported god knows how many .jar files into my eclipse package. Does anyone have a good example on how to connect to a url, send/receive information (even login), parse it, and send more information? I understand that I'm asking for a lot. I don't need all the answers, good documentation and some good examples would make me soooo happy.
Start with http://hc.apache.org/
Then look at http://code.google.com/p/google-gson/
or: http://wiki.fasterxml.com/JacksonHome
That should be all you need.
Found a really solid example here on this blog http://www.gnovus.com/blog/programming/making-http-post-request-json-using-apaches-httpclient
Pasted below if for some reason the link doesnt work.
public class SimpleHTTPPOSTRequester {
private String apiusername;
private String apipassword;
private String apiURL;
public SimpleHTTPPOSTRequester(String apiusername, String apipassword, String apiURL) {
this.apiURL = apiURL;
this.apiusername = apiusername;
this.apipassword = apipassword;
}
public void makeHTTPPOSTRequest() {
try {
HttpClient c = new DefaultHttpClient();
HttpPost p = new HttpPost(this.apiURL);
p.setEntity(new StringEntity("{\"username\":\"" + this.apiusername + "\",\"password\":\"" + this.apipassword + "\"}",
ContentType.create("application/json")));
HttpResponse r = c.execute(p);
BufferedReader rd = new BufferedReader(new InputStreamReader(r.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
//Parse our JSON response
JSONParser j = new JSONParser();
JSONObject o = (JSONObject)j.parse(line);
Map response = (Map)o.get("response");
System.out.println(response.get("somevalue"));
}
}
catch(ParseException e) {
System.out.println(e);
}
catch(IOException e) {
System.out.println(e);
}
}
}

Categories

Resources