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);
    }
%>
user2986084
  • 98
  • 4
  • 12

8 Answers8

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

0

use Code:

String returnString = jo.toString();

and send as response.

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.

Ezzored
  • 905
  • 4
  • 10
0

use JSONObject

JSONObject jsonObj = new JSONObject("String");
Nambi
  • 11,944
  • 3
  • 37
  • 49
0

This should do the trick

JSONObject jsonObject = JSONObject.fromObject(jo);
UrMo
  • 1
  • 1
0
// String to Json Object
JSONObject jsonObject = new JSONObject(stringResponse);

// JsonObject to String
String string = jsonObject.toString();
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
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");

Sujith PS
  • 4,776
  • 3
  • 34
  • 61
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());
Thirumalvalavan
  • 2,660
  • 1
  • 30
  • 32