0

I have json object named 'jo' in the below code.I convert this json object to java string to return this json object as response.Then how i convert this string back to json object in the required format.Please help me.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*,java.util.ArrayList"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<%
    JSONArray cellarray = new JSONArray();
    JSONObject cellobj = null; //new JSONObject();
    JSONObject jo=new JSONObject();
    String country=request.getParameter("count");  
    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();  
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:
        3306/test","root","root");  
        Statement stmt = con.createStatement();  
        ResultSet rs = stmt.executeQuery("Select * from state 
        where countryid='"+country+"'");  
            while(rs.next()){
                cellobj = new JSONObject();
                cellobj.put("id", rs.getString(1));
                cellobj.put("name", rs.getString(3));
                cellarray.add(cellobj);
            }  
            jo.put("arrayName",cellarray);
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jo.toString());
        }
    catch(Exception e){
        System.out.println(e);
    }
%>
4
  • Take a look at stackoverflow.com/questions/1395551/…. Commented Jan 3, 2014 at 11:02
  • jo = (JSONObject) jo; ? Commented Jan 3, 2014 at 11:06
  • Just an off-topic note: scriptlets were already a maintenance nightmare 10 years ago; I would really invest time into learning how to use servlets & JSPs together to get clean, maintaintable code and NO java code in your JSPs. Commented Jan 3, 2014 at 11:07
  • Please see this link, it may help stackoverflow.com/a/44389185/1404798 Commented Jun 6, 2017 at 11:53

8 Answers 8

2

Your questions seems a bit messy + formating of your code is messy too. But as i understood you, in order to convert String back to JSONObject you can do

String someString= getResponseFromServer();
JSONObject jsonObject = new JSONObject(someString);
// and then do with jsonObject variable whatever you desire

Also as people sugested you can use java libraries such as GSON or JSONLib etc

Sign up to request clarification or add additional context in comments.

Comments

0

use Code:

String returnString = jo.toString();

and send as response.

2 Comments

I wan't to convert this string back to json object.Give me the code for that.
JSONObject jsonObj = new JSONObject(returnString);
0

You can use various libraries for this task - i recommend using GSON. You can find good tutorials on their site too, here.

I would use a custom object, not a simple JSONObject - you can deserialize and store your objects this way easily.

Comments

0

use JSONObject

JSONObject jsonObj = new JSONObject("String");

Comments

0

This should do the trick

JSONObject jsonObject = JSONObject.fromObject(jo);

Comments

0
// String to Json Object
JSONObject jsonObject = new JSONObject(stringResponse);

// JsonObject to String
String string = jsonObject.toString();

Comments

0

Wherever you are receiving response , you can use the following code :

 JSONObject json= new JSONObject ( responseString );

Then you can process the same as Json object .

Like String id=json.put("id");

Comments

0

It is just a suggestion, I also faces like this issue.

Finally, I tried with GSON library. GSON is directly convert your JSONString to java class object.

Example:

String jsonString = {"phoneNumber": "8888888888"}

create a new class:

class Phone {

@SerializedName("phoneNumber")
private String phoneNumebr;


public void setPhoneNumber(String phoneNumebr) {
this.phoneNumebr = phoneNumebr;
}

public String getPhoneNumebr(){
return phoneNumber;
}

}

// in java

Gson gson = new Gson();
Phone phone = gson.fromJson(jsonString, Phone.class);

System.out.println(" Phone number is "+phone.getPhoneNumebr());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.