Showing posts with label JSONArray. Show all posts
Showing posts with label JSONArray. Show all posts

Monday, 29 March 2021

Javax.json: Get list from JsonArray

Step 1: Get list view of elements from json array.

List<JsonString> jsonStrings = empNamesJsonArr.getValuesAs(JsonString.class);

 

Step 2: Populate list from list view data.

for (JsonString j : jsonStrings) {

         empNames.add(j.getString());

}

 

Find the below working application.

 

JsonArrayToListDemo.java

package com.sample.app;

import java.util.ArrayList;
import java.util.List;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonString;

public class JsonArrayToListDemo {
	public static void main(String[] args) {
		JsonArrayBuilder empNamesArrayBuilder = Json.createArrayBuilder();

		empNamesArrayBuilder.add("Ram");
		empNamesArrayBuilder.add("Rahim");
		empNamesArrayBuilder.add("Joel");
		empNamesArrayBuilder.add("PTR");

		JsonArray empNamesJsonArr = empNamesArrayBuilder.build();

		String json = empNamesJsonArr.toString();

		System.out.println(json);

		List<String> empNames = new ArrayList<>();

		List<JsonString> jsonStrings = empNamesJsonArr.getValuesAs(JsonString.class);

		for (JsonString j : jsonStrings) {
			empNames.add(j.getString());
		}

		empNames.stream().forEach(System.out::println);

	}
}

 

Output

["Ram","Rahim","Joel","PTR"]
Ram
Rahim
Joel
PTR

 

 

 

 

Previous                                                    Next                                                    Home

Saturday, 16 March 2019

Produce JSONArray of json object from comma separated string


Step 1: Create a string such that comma separated headers followed by comma separated data.

String data = "id, name, organization \n" +
                    "1, Krishna, ABC Corporation \n" +
                    "2, Ram, XYZ Corporation \n" +
                    "3, Chamu, TUV Organization";

Step 2: Use ‘CDL.toJSONArray’ to get the JSONArray from given string data.

JSONArray jsonArray = CDL.toJSONArray(data);


[{
         "name": "Krishna",
         "id": "1",
         "organization": "ABC Corporation"
}, {
         "name": "Ram",
         "id": "2",
         "organization": "XYZ Corporation"
}, {
         "name": "Chamu",
         "id": "3",
         "organization": "TUV Organization"
}]

App.java
package com.sample;

import org.json.CDL;
import org.json.JSONArray;

public class App {

 public static void main(String args[]) {
  String data = "id, name, organization \n" +
      "1, Krishna, ABC Corporation \n" +
      "2, Ram, XYZ Corporation \n" +
      "3, Chamu, TUV Organization";
  
  JSONArray jsonArray = CDL.toJSONArray(data);
  
  System.out.println(jsonArray);

 }
}

Output
[{"name":"Krishna","id":"1","organization":"ABC Corporation"},{"name":"Ram","id":"2","organization":"XYZ Corporation"},{"name":"Chamu","id":"3","organization":"TUV Organization"}]

As you see in the above example, I supplied both the headers and data in same string. You can even separate headers from data in below way.

String data = "1, Krishna, ABC Corporation \n" +
                    "2, Ram, XYZ Corporation \n" +
                    "3, Chamu, TUV Organization";

JSONArray jsonArray = new JSONArray();
jsonArray.put("id");
jsonArray.put("name");
jsonArray.put("organization");

jsonArray =  CDL.toJSONArray(jsonArray, data);


App.java
package com.sample;

import org.json.CDL;
import org.json.JSONArray;

public class App {

 public static void main(String args[]) {
  String data = "1, Krishna, ABC Corporation \n" +
      "2, Ram, XYZ Corporation \n" +
      "3, Chamu, TUV Organization";
  
  JSONArray jsonArray = new JSONArray();
  jsonArray.put("id");
  jsonArray.put("name");
  jsonArray.put("organization");
  
  jsonArray =  CDL.toJSONArray(jsonArray, data);
   
  System.out.println(jsonArray);

 }
}



Previous                                                 Next                                                 Home

Convert JSONArray to comma separated string


CDL.rowToString(jsonArray) method returns a comma separated string from JSONArray.

JSONArray jsonArray = new JSONArray();
jsonArray.put("Chess");
jsonArray.put("Tennis");
jsonArray.put("Cricket");
jsonArray.put("Football");

String str = CDL.rowToString(jsonArray);

App.java
package com.sample;

import org.json.CDL;
import org.json.JSONArray;

public class App {

 public static void main(String args[]) {
  JSONArray jsonArray = new JSONArray();
  jsonArray.put("Chess");
  jsonArray.put("Tennis");
  jsonArray.put("Cricket");
  jsonArray.put("Football");
  
  String str = CDL.rowToString(jsonArray);

  System.out.println(str);

 }
}

Output
Chess,Tennis,Cricket,Football



Previous                                                 Next                                                 Home