Reputation: 25
JSONArray jsonArray = jo.getJSONArray("products_list");
productList = new ArrayList<>();
String ProductName = null;
String ProductQTY = null;
String ProductCost = null;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
ProductName = jsonObject2.getString("product_name");
ProductQTY = jsonObject2.getString("product_quantity");
ProductCost = jsonObject2.getString("product_cost");
}
Pname = ProductName;
Pqty = ProductQTY;
Pcost = ProductCost;
SaveProduct(BillNo, Pname, Pqty, Pcost);
and the corresponding json is-
"gstin_no": "987654321",
"products_list": [
{
"product_name": "himalaya shampoo",
"product_quantity": "20",
"product_cost": "100*product_quantity"
},
{
"product_name": "almonds",
"product_quantity": "2",
"product_cost": "400*product_quantity"
},
{
"product_name": "Rin Soap",
"product_quantity": "20",
"product_cost": "10*product_quantity"
},
{
"product_name": "himalaya shampoo",
"product_quantity": "20",
"product_cost": "100*product_quantity"
}]
I am trying to make an android app where i am scanning QR code and getting output in json format i want to send an array with multiple objects into Mysql database but when i am sending value to database i am value of last object is inserting in database . how can i insert all the objects of array into database??
How can I insert all the objects to the Mysql databse.
Upvotes: 0
Views: 1577
Reputation: 119
I suggest Gson Library for better readable code
First, add this to your dependencies
implementation 'com.google.code.gson:gson:2.8.2
Second, create your product model class
public class Product {
private String product_name;
private String product_quantity;
private String product_cost;
}
just change your code with these lines
Gson gson = new Gson();
Type listType = new TypeToken<List<Product>>() {}.getType();
List<Product> productsList = new Gson().fromJson(jsonArray, listType);
then you can save productsList in database better code with a few lines and better performance
Upvotes: 0
Reputation: 38439
So you need to make save call on your loop (json array length).
String BillNo = jo.optString("gstin_no");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.optJSONObject(i);
String ProductName = jsonObject2.optString("product_name");
String ProductQTY = jsonObject2.optString("product_quantity");
String ProductCost = jsonObject2.optString("product_cost");
SaveProduct(BillNo, ProductName, ProductQTY, ProductCost);
}
Upvotes: 0
Reputation: 409
you are calling SaveProduct(BillNo, Pname, Pqty, Pcost);
after the for loop.so you are getting last index values.Make sure your method should be inside the loop.
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
ProductName = jsonObject2.getString("product_name");
ProductQTY = jsonObject2.getString("product_quantity");
ProductCost = jsonObject2.getString("product_cost");
SaveProduct(BillNo, ProductName, ProductQTY, ProductCost);
}
Upvotes: 2