0

I want to create json object as, {"jsonName":[{"id":"1","name":"abc"},{"id":2,"name":"pqr"}]} "jsonName" fetch the data from database and I want to create the object from that data. and I want to update some value from that json object with "id" after we created that.

Suppose I run the query from java..

String selectStr = "select * from emp";
ResultSet rs = ps.executeQuery ();
while(rs.next())
{
.....
}

I'm fetching data like this and wants to create and json object that store all the data rows. can someone please help me .. I'm new to json object.

3 Answers 3

1

Using org.json library:

JSONArray elements  = new JSONArray();
JSONObject rootJson = new JSONObject();
String selectStr    = "select * from emp";
try
{
    JSONArray elements  = null; // to prevent query returning empty resultset
    JSONArray el        = new JSONArray();

    ResultSet rs = ps.executeQuery();
    while(rs.next())
    {
        JSONObject el = new JSONObject();
        el.put("id", rs.getInt(1));
        el.put("name", rs.getString(2));
        elements.put(element);
    }

    rootJson.put("jsonName", elements);

}catch(Exception e)
{
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can try quick-json. Which can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. quick-json can work with any arbitrary Java objects.

Comments

0

When using a library such as gson (see: https://github.com/google/gson) doing this becomes a trivial task. You can simply define a structure in the shape of classes such as this:

public class JsonObj {
    public List<JsonItem> jsonName;
}

public class JsonItem {
    public int id;
    public String name;
}

Then, you can use gson to serialize your object into json (exhaustive example, but you get the point):

Gson gson = new Gson();
JsonObj obj = new JsonObj();
obj.jsonName = new ArrayList<String>();

String selectStr = "select * from emp";
ResultSet rs = ps.executeQuery();
while(rs.next())
{
    JsonItem item = new JsonItem();
    item.id = rs.getInt(1); //or whatever column index goes here
    item.name = rs.getString(2); //or whatever column index goes here
    obj.jsonName.Add(item);
}
String json = gson.toJson(obj);

json object will now contain the serialized json in String format.

7 Comments

You are creating JsonItem two times, If we have data 100 times then? can we create this using loop? please tell me
Of course you can, you can make it as wild as you want. Althought at that point it's better to substitute the array for a list (both serialize to an array, but working with an arraylist is easier than creating an array of all the objects). I'll update my code in the post accordingly.
Thanks @nbokmans for your help. but if we are going to fetch the data from database for id,name then what will be the changes to do ? can you please give me some code.
Well that depends on how you fetch data from the database. That's a separate problem/piece of functionality. Can you update your question to show some example data from the database and how you fetch it?
hey I have updated my question can you please look it into that?
|

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.