0

Am retrieving information from my SQLite database to display on CardView

My SQLite database structure is SQLite DB

My class is

 public class ServiceRequest{
    public String reqid;
    public String name;
    public String branch;
    public Date date;
    public Date time;
    public String services;
    //Getter and setter
    .............
    .............
   }

I can convert this to JSON format using

    List<ServiceRequest> reqs = getAllReqs();
    List<ServiceRequest> jobservList = new ArrayList<>();

    for (ServiceRequest access : reqs) {
        ServiceRequest ob = new ServiceRequest();

        ob.setId(access.getId());
        ob.setBranch(access.getBranch());
        ob.setName(access.getName());
        ob.setDate(access.getDate());
        ob.setTime(access.getTime());
        ob.setServices(access.getServices());
        jobservList.add(ob);
    }

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json2 = gson.toJson(jobservList);
    return json2;

but my desired JSONObject format is

{  
 "100": {

        "name": "Rahul Suresh",
        "branch": "Koramangala",
        "phNumber":"123456",
        "date": "2016-08-06",
        "time": "16:00",
        "reqServices": "Loans"
       },
 "200": {
         "name": "Sidh",
         "branch": "Jayanagar",
         "phNumber":"182694",
         "date": "2016-08-12",
         "time": "11:00",
         "reqServices": "OpenAcc,SafeDeposit"
        }
  }

so that I will get one whole JSON object with a single call

JSONObject jb = (JSONObject) jsonObject.get(Integer.toString(id));

100,200 are 'reqid' s

It's possible to achieve this using string builder. But is there any other ways to implement this like using an object mapper along with a class or something..?

4
  • Your class structure is not correct if you want to use an object mapper like Gson or Jackson. The ID would have to be in the inner object, not the key Commented Aug 5, 2016 at 22:29
  • @cricket_007 Ya am aware of that...Since I want JSON on the given format am searching a way other than making a string builder. Commented Aug 5, 2016 at 22:54
  • It is possible with a Hashmap of integer to your class, then have Gson convert that Commented Aug 5, 2016 at 23:27
  • @cricket_007 Can you gimme a small sketch how that class will be?!! Commented Aug 6, 2016 at 0:53

1 Answer 1

1

If you would like to form the JSON you have shown, you could "pull out" the ID into a HashMap key, then set the value to be your object.

I can't remember how Gson handles the conversion of the object values in the map, but this is the general idea

List<ServiceRequest> reqs = getAllReqs();

HashMap<Integer, ServiceRequest> map = new HashMap<Integer, ServiceRequest>();
for (ServiceRequest access : reqs) {
    map.put(access.getId(), access);
}

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json2 = gson.toJson(map); // TODO: Not sure if this will work
return json2;
Sign up to request clarification or add additional context in comments.

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.