Do somthings

In this tutorial, we learn about how to convert JsonArray into Jsonobject. but first, we know about what is JsonArray and JsonObject

JSON Array:

JSON array represents an ordered list of values. JSON array can store multiple values. It can store string, number, boolean, or object in a JSON array. In JSON array, values must be separated by comma. The [ (square bracket) represents JSON array.

Let’s see an example of JSON arrays storing string values.

["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

Let’s see an example of JSON arrays storing number values.

[12, 34, 56, 43, 95]

Let’s see a simple JSON array example having 4 objects.

 {"employees":[
{"name":"Ram", "email":"[email protected]", "age":23},
{"name":"Shyam", "email":"[email protected]", "age":28},
{"name":"John", "email":"[email protected]", "age":33},
{"name":"Bob", "email":"[email protected]", "age":41}
]}

JsonObject

JSON object holds key/value pair. Each key is represented as a string in JSON and value can be of any type. The keys and values are separated by a colon. Each key/value pair is separated by a comma.The curly-brace { represents JSON object.

Let’s see an example of a JSON object.

{
"employee": {
"name": "sonoo",
"salary": 56000,
"married": true
}
}

 I think you understand the difference between object and array. now we move to the main part. let’s suppose this is a JSON Array to convert into a JSON object.

[


{

"product_name":"Product1",
"price":"50",
"discount":"10",
"price_after_discount":"40",
"quantity":"1",
"created_at":"2022-02-07T11:52:40.000000Z",
},
{
"product_name":"Product2",
"price":"550",
"discount":"10",
"price_after_discount":"500",
"quantity":"3",
"created_at":"2022-02-07T11:52:40.000000Z",

},
{

"product_name":"Product3",
"price":"1500",
"discount":"10",
"price_after_discount":"1400",
"quantity":"8",
"created_at":"2022-02-07T11:52:37.000000Z",

},
{
"product_name":"Product4",
"price":"1000",
"discount":"10",
"price_after_discount":"900",
"quantity":"6",
"created_at":"2022-02-07T11:52:37.000000Z",

}


]

then you can try this code of pice

    //Import packages and classes
package "Your pakege name".JavaObjectToJSON;
import org.json.JSONArray;
import org.json.JSONObject;
//Creating JSONArrayToArrayList class
public class JSONArrayToArrayList {
public static void main(String[] args){
//Creating string of JSON data
String jsonData = " [{ \"product_name\":\"Product1\", \"price\":\"50\", \"discount\":\"10\", \"price_after_discount\":\"40\", \"quantity\":\"1\", \"created_at\":\"2022-02-07T11:52:40.000000Z\",},\n" +
" { \"product_name\":\"Product2\", \"price\":\"550\", \"discount\":\"10\", \"price_after_discount\":\"500\", \"quantity\":\"3\", \"created_at\":\"2022-02-07T11:52:40.000000Z\",},\n" +
" { \"product_name\":\"Product3\", \"price\":\"1500\", \"discount\":\"10\", \"price_after_discount\":\"1400\", \"quantity\":\"8\", \"created_at\":\"2022-02-07T11:52:37.000000Z\",},\n" +
" { \"product_name\":\"Product4\", \"price\":\"1000\", \"discount\":\"10\", \"price_after_discount\":\"900\", \"quantity\":\"6\", \"created_at\":\"2022-02-07T11:52:37.000000Z\",}]\n";

//Converting jsonData string into JSON object
JSONArray jsnobject = new JSONArray(jsonData);

//Printing JSON object..
System.out.println("JSON OBJECT: "+jsnobject);

//now here runing a loop then get all products..
for(int i=0; i<JSONArray.size(); i++) {

JSONObject object = JSONArray.getJSONObject(i);
String Name = object.getString("product_name");
String Price = object.getString("price");
String Discount = object.getString("discount");
String Real_price = object.getString("price_after_discount");
String Quantity = object.getString("quantity");
String Date = object.getString("created_at");

//now print all the values
System.out.println("Producr name:"+Name);
System.out.println("Producr Price:"+Price);
System.out.println("Discount:"+Discount);
System.out.println("Real Price:"+Real_price);
System.out.println("Quantity:"+Quantity);
System.out.println("Date:"+Date);
}
}
}
Categories: JAVA

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *