A
A
Awesome_T2020-07-02 14:55:45
Java
Awesome_T, 2020-07-02 14:55:45

How to assign a number to a JSONObject?

Hi all!
Please help me to solve a small problem.
After executing the request to the server, the objects that I try to save in SQLlite come.
however, instead of sequentially saving each of the objects, the first object from the request is multiple written

2020-07-02 14:45:52.262 28975-28975 D/CleanersActivity: Connection response to HTTP request Get_Usage_Cleaner: {"error":false,"message":"Data was downloudaed!","\u0441leaner_usage":{"id_cleaner ":"ID01","datetime_cleaner_usage":"2020-05-30","water_spent_cleaner_usage":"20","electric_spent_cleaner_usage":"123"}}{"error":false,"message":"Data was downloudaed !","\u0441leaner_usage":{"id_cleaner":"ID01","datetime_cleaner_usage":"2019-05-22","water_spent_cleaner_usage":"29","electric_spent_cleaner_usage":"59"}}{"error" :false,"message":"Data was downloudaed!","\u0441leaner_usage":{"id_cleaner":"ID01","datetime_cleaner_usage":"2019-05-27","water_spent_cleaner_usage":"35","electric_spent_cleaner_usage":"129"}}{"error":false,"message":" Data was downloudaed!","\u0441leaner_usage":{"id_cleaner":"ID01","datetime_cleaner_usage":"2020-06-28","water_spent_cleaner_usage":"22","electric_spent_cleaner_usage":"96"}}id_cleaner":"ID01","datetime_cleaner_usage":"2020-06-28","water_spent_cleaner_usage":"22","electric_spent_cleaner_usage":"96"}}id_cleaner":"ID01","datetime_cleaner_usage":"2020-06-28","water_spent_cleaner_usage":"22","electric_spent_cleaner_usage":"96"}}
2020-07-02 14:45:52.264 28975-28975 D/CleanersActivity: Number of objects
4 ---
2020-07-02 14:45:52.295 28975-28975 D/DatabaseHandler: Response Insert_Cleaner_Usage:water_spent_cleaner_usage=20 datetime_cleaner_usage=2020-05-30 electric_spent_cleaner_usage=123 id_cleaner=ID01
2020-07-02 14:245 -28975 D/DatabaseHandler: ----- Method for inserting data into local table TABLE_CLEANERS_USAGE ----
2020-07-02 14:45:52.316 -30 electric_spent_cleaner_usage=123 id_cleaner=ID01
2020-07-02 14:45:52.318 28975-28975 D/DatabaseHandler: -----Method for inserting data into local table TABLE_CLEANERS_USAGE ----
2020-07-02 14:45:52.333 28975-28975 D/DatabaseHandler: Response Insert_Cleaner_Usage:water_spent_cleaner_usage=20 datetime_cleaner_usage=2020-05-30 electric_spent_cleaner_usage=123 id_cleaner=ID01
2020-07-02 14:45:52.335 28975-28975 D/DatabaseHandler: -----Method to insert data into local table TABLE_CANUSAGE_USAGE --
2020-07-02 14:45:52.348 28975-28975 D/DatabaseHandler: Insert_Cleaner_Usage:water_spent_cleaner_usage=20 datetime_cleaner_usage=2020-05-30 electric_spent_cleaner_usage=123 id_cleaner=ID01 response


The code,

public void onResponse(String response) {
                        Log.d(TAG, "Ответ соединения на HTTP  запрос  Get_Usage_Cleaner: " + response);
                        //hideDialog();
                        try {
                            JSONObject jObj = new JSONObject(response);
                            boolean error = jObj.getBoolean("error");
                            if (!error) {
                                JSONObject json_сleaner_usage = jObj.getJSONObject("сleaner_usage");
                                Log.d(TAG, "Количество обънетов " + json_сleaner_usage.length());
                               for (int i = 0; i < json_сleaner_usage.length(); i++) {
                                    db.Insert_Cleaner_Usage(json_сleaner_usage.getString(KEY_ID_CLEANER),
                                            json_сleaner_usage.getString(KEY_WATER_SPENT_CLEANER_USAGE),
                                            json_сleaner_usage.getString(KEY_ELECTRIC_SPENT_CLEANER_USAGE),
                                            json_сleaner_usage.getString(KEY_DATETIME_CLEANER_USAGE));
                                }
                            }


How can the object number be accessed to store all objects from a query?

I think it should look something like this (I could be wrong of course)

json_сleaner_usage.COUNT(i).getString(KEY_ID_CLEANER),


Thanks in advance for your help)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2020-07-02
@Awesome_T


1 strange json , this is neither
an array of objects nor fields in the object

JSONArray array = root.getJSONArray("some id");
for (int i = 0; i < array.length(); i++) {
    JSONObject item = array.getJSONObject(i);
    db.Insert_Cleaner_Usage()
}

if the fields are in the object then so
JSONObject list = root.getJSONObject("some id");
Iterator<String> iterator = list.keys();
while(iterator.hasNext()) {
    String key = iterator.next();
    JSONObject item = list.getJSONObject(key);
    db.Insert_Cleaner_Usage()
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question