Simple JSON-String Query - java

I am developing a web-app using AJAX requests on the client-side and Servlets on the server-side.
My aim is to send objects of Javascript to server, then do some manipulations there and send it back to show here.
Let's say my js object is
var obj={hero:"Spiderman",name:"Peter Parker"};
My Approach
1.Convert obj to JSON string and send
var str= JSON.stringify(obj);
xmlhttp.open("POST",myurl,true);
xmlhttp.setRequestHeader("Content-Type","application/json",true);
xmlhttp.send("data="+str);
2. Recieve string,convert this back to JSON, manipulate "name" to "Bruce Wayne" and send it back as string
3.Recieve and convert back to Json
var data= JSON.parse(xmlhttp.responseText);
I am struggling at second point.I am using org.json for it .I searched and read docs but could not find satisfied answer for converting string to json and vica-versa in JAVA in my context.
It would be really helpful one could provide simple working code or point to some links where I can study.
P.S :
I cannot use Jquery as I am using AngularJS. See Why?
I will always send valid JSON string.
I can use other JSON lib. if its good than org.json and satisfy my needs.
Please provide its jar download link.

Assuming you are able to pull out data in your server code
This is how you can do it using org.json:
JSONParser parser = new JSONParser();
JSONObject requestObj = (JSONObject) parser.parse(data);
String name = (string)requestObj.get("name");
name = "Bruce Wayne";
Code to create the response can look something like this:
JSONObject response = new JSONObject();
response.put("name",name);
return response.toJSONString();
This assumes your server method returns a String type
And in case if you are using Servlet you can use HttpServletResponse object res to create response like:
res.setContentType("application/json");
OutputStream os = res.getOutputStream();
os.write(response.toString().getBytes());
os.close();

Related

Json Parser not working on request mapping

Problem:
I am unable to parse json request to an object containing double quotes in it.
For example:
jsonString = {
"desc":"Hello stackOverFlow, please reach on this email "asdas#gmail.com". thanks";
}
When I am trying to convert this I am not able parse to an variable, because it looks like invalid json but in real time the request has double quotes in it.
Please show me some good parsing techniques which can do parse this type of requests.
Thanks
You have to escape all of the double quotes, so for example:
String json = "\"{\"desc\":\"Hello stackOverFlow, please reach on this email \"asdas#gmail.com\". thanks\"}";
As you can see it is a lot of work to create very simple JSON, so you are better of using some library for that. I recommend you org.json, it is very lightweight and easy to use. Using it, it would look like this:
JSONObject json = new JSONObject();
json.put("description", "Your description....");
String jsonString = json.toString();

Android - Accessing JSON children from a URL

I'm in the process of converting my website to an Android app and one of the pages' data currently is populated via JSON in my website. The way it works is that the URL generates a different JSON data with the same structure based on the passed ID. I already have the logic for passing the ID to the URL. Now I want to read the data through Java code and parse the JSON children and its values in it.
I have a URL that leads to the JSON file in textual form, but I'm not sure how to go about reading the data from it and accessing the child nodes based on the JSON key.
So I guess what I'm asking is what is the usual approach for this procedure? I see a lot of different examples, but none of which are applicable to my problem.
Anyone have any suggestions as to how I should approach this?
JSONObject = new JSONObject(yourjsonstring);
Now you have your Json Object...
If your Json start with array use this:
JSONArray = new JSONArray(yourjsonarray);
You can use existing libraries to parse JSON, gson or Moshi are two solutions.
The way you go about parsing the JSON is as followed
First you need to make pojo's with the same structure as the JSON file.
then you can parse it to java code via the fromJSON() method, this will make new objects and fill it with the data from the JSON.
gson example for clarification:
Gson gson = new Gson();
Response response = gson.fromJson(jsonLine, Response.class);
where jsonLine = your json file and the Response.Class the pojo in which you want to json to load.
Now you have the JSON values as Java classes in response.
If you're using Retrofit and OkHTTP to perform the network calls i suggest you use Moshi as it's also from Square and claimed to work faster and better than gson. (if you want to know why you can leave a comment).
I think what you're trying to do is this
on post execute method do the following
#Override
protected void onPostExecute(String result) {
String status = "";
String message = "";
String tag = "";
String mail = "";
try {
JSONObject jsonResult = new JSONObject(result);
status = jsonResult.optString("status");
message = jsonResult.optString("message");
tag = jsonResult.optString("tag");
mail = jsonResult.optString("mail");
} catch (JSONException e) {
e.printStackTrace();
}
of course your json array contains different keys
Just reolace them with yours

Parsing Request as json with stackexchange API

I am struggling with some issue related with http, java and the stackexchange API
consider the following url as string:
private static final String URLSTRING_2 = "http://freegeoip.net/json/";
if I write this url in my browser I get this answer as json:
now im trying to do that with java and only native libs, for that am using the snippet below wich is working so far so good...
If I parse the json and i try to get the value for the key "country_name" then the snippet prints as spected "Singapore"
public static void main(String[] args) throws Exception {
// Connect to the URL using java's native library
final URL url = new URL(URLSTRING_2);
final HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
// Convert to a JSON object to print data
final JsonParser jp = new JsonParser(); // from gson
final JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); // Convert the input stream to a json
// element
final JsonObject rootobj = root.getAsJsonObject(); // May be an array, may be an object.
final String country = rootobj.get("country_name").getAsString(); // just grab the zipcode
System.out.println("country_name: " + country);
}
Now my question:
if I do the same with this link
https://api.stackexchange.com//2.2/users/22656?order=desc&sort=reputation&site=stackoverflow&filter=!T6oHjO_RIWkfWpoL5g
my browser outputs the following json:
but if I try to parse the json I get an exception because am getting from the request this:
ý•#‡ž¼ÚRìØ1ôX`»v?±h[‹-¹’/+ò........
for something that is not even human readable...
do you know why?
Thanks in advance
The StackOverflow API GZIP-compresses its response. The reason you see that string of non-human-readable characters is that you are trying to read GZIP-compressed data without first decompressing it.
Your browser is capable of reading this header and doing the decompression itself. Your code isn't yet.
You can confirm that GZIP compression is used by displaying the value of the Content-Encoding header in the response. Adding the line
System.out.println(request.getContentEncoding());
will print out
gzip
Fortunately, fixing the problem is fairly straightforward. You need to wrap the InputStream you get from the request in a GZIPInputStream:
final JsonElement root = jp.parse(new InputStreamReader(new GZIPInputStream((InputStream) request.getContent()))); // Convert the input stream to a json
However, instead of the built-in Java classes, I'd recommend using a library such as Apache HTTPComponents Client for making your HTTP requests. In particular, a library such as this will automatically detect the content-encoding and do the decompression for you.

java JSON text encoding issue

In my application I retrieve search results in JSON format from an external tool called Google Search Appliance(GSA).
The JSON result from GSA is very large and therefore I prefer to modify the GSA JSON result into something more suitable for displaying on my webpage.
If I directly display the GSA JSON result without formatting it in my java code I'm not facing any encoding issues on my webpage.
But if I format the large GSA JSON result into a suitable JSON format in my servlet java code I'm facing encoding problems.
Example - “All Access Pass” gets displayed as ÂAll Access PassÂ.
I return the modified json from my servlet to the webpage use the following code -
response.setContentType("application/json;charset=UTF-8");
I have tried to change the charset to iso-8859-1 but it does not make any difference.
I edit my original JSON in the following manner -
String responseText = getMethod.getResponseBodyAsString();
JSONObject resultJSON = new JSONObject();
try {
JSONObject jsonObj = new JSONObject(responseText);
JSONArray resultJsonArray = jsonObj
.getJSONArray("RES");
JSONObject searchResultJSON = null;
for (int iCnt = 0; iCnt < resultJsonArray.length(); iCnt++) {
searchResultJSON = new JSONObject();
JSONObject obj = resultJsonArray.getJSONObject(iCnt);
JSONObject metaTagObj = obj
.getJSONObject("MT");
if (metaTagObj.has(("title"))) {
searchResultJSON.put("title",metaTagObj.get("title").toString());
}
resultJSON.accumulate("RES", searchResultJSON);
}
response.setContentType("application/json;charset=UTF-8");
response.getWriter().print(resultJSON);
}catch(JSONException e){}
The modification to the original JSON which I'm going here can be done in JavaScript which would solve my problem but it is something which I do not want to do.
Is there a way to find out the encoding format of the text in the original GSA JSON?
How can I avoid the java code from changing the text encoding in the original GSA JSON?
Please help me understand what is going on here and how I can avoid this problem.
The text encoding problem was happening because the call which is made to the GSA server using Apache HTTP Client was using a default content encoding character set of iso-8859-1 but the GSA server expected the HTTP Client request and response to be in UTF-8 encoding.
This problem got resolved after setting the encoding for HTTPClient -
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("UTF-8");
And the servlet response encoding to
response.setContentType("application/json;charset=UTF-8");

How to send JSON array from server to client, i.e. (java to AJAX/Javascript)?

I have the following JSON array in my JSON.java file:
ArrayList array=new ArrayList();
array.add("D");
array.add("A");
array.add("L");
I would like to send array to the AJAX script located in AJAX.jsp.
I know how to receive the text in AJAX via e.g.
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
But I don't know how to send the array from the server to the client!
Appreciate your help
ok First:
ArrayList array=new ArrayList();
array.add("D");
array.add("A");
array.add("L");
JSONArray array = new JSONArray();
This can't compile... you have a duplicate variable array ;-)
Second: create a servlet/Struts Action/etc that will contains the code that will create your array. Then transform it in JSON with a JSON library. Finally, put the string in the response of your servlet/Struts Action/etc.
Use JQuery to ease your effort on the Ajax call. It will help you with the Ajax call and the transformation back to Json from the string received in the http response.
Ex:
your ajax call should be like that (with JQuery)
$.getJSON("http://yourserver/JSONServlet",
function(data){
alert (data) // this will show your actual json array
});
});
and your servlet should look like that:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import net.sf.json.JSONArray;
public class JSONServlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
JSONArray arrayObj=new JSONArray();
arrayObj.add("D");
arrayObj.add("A");
arrayObj.add("L");
arrayObj.add("D");
arrayObj.add("A");
arrayObj.add("TEST");
PrintWriter out = response.getWriter();
out.println(arrayObj);
for(int i=0;i<arrayObj.size();i++){
out.println(arrayObj.getString(i));
}
}
}
you basically use certain classes in java like the ones defined here: http://json.org/java/ convert the final output to a json string and then send it to javascript there you convert the json string back to json using eval or probably using a library called json2.js and you are all set..
here is code for the same:
JSONObject obj=new JSONObject();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
StringWriter out = new StringWriter();
obj.writeJSONString(out);
String jsonText = out.toString();
System.out.print(jsonText);
for more http://code.google.com/p/json-simple/wiki/EncodingExamples
Instead of using org.json's barebones library as suggested already, consider using data-binding JSON library like Jackson or GSON (there are many others as well), in which case you can simplify servlet case (with Jackson) to:
new ObjectMapper().writeValue(response.getOutputStream(), array);
and that's all you need.
And for even simpler handling, JAX-RS services (Jersey, RESTeasy, CXF) can further simplify handling, to reduce code you need compared to raw servlets. JAX-RS + JSON is the modern way to implement web services on Java, so might make sense to learn it now.
The simplest way is construct a json string in java file and return that json string to client.
Javascript provides a eval() method which inverts json string to json object.
Then you can perform what ever operations you need.

Categories

Resources