To Generate the JSON object of below formate using json.jar
{ "employees": { {"firstName": "John", "lastName": "Doe"}, {"firstName": "Anna", "lastName": "Smith"}, {"firstName": "Peter", "lastName": "Jones"} }, "manager": [ {"firstName": "John", "lastName": "Doe"}, {"firstName": "Anna", "lastName": "Smith"}, {"firstName": "Peter", "lastName": "Jones"} ],
}
import org.json.JSONArray;
import org.json.JSONObject;
public JSONObject getJsonResponse(){
JSONObject employees = new JSONObject();
employees.put(getPerson("John","Doe"));
employees.put(getPerson("Anna","Smith"));
employees.put(getPerson("Peter","Jones"));
JSONArray managers = new JSONArray();
managers.put(getPerson("John","Doe"));
managers.put(getPerson("Anna","Smith"));
managers.put(getPerson("Peter","Jones"));
JSONObject response= new JSONObject();
response.put("employees", employees );
response.put("manager", managers );
return response;
}
---------------------------------------------------------------------------------------------------------------------
To Generate the JSON object of below formate using Gson.jar
[
{
"staf": [
{
"emp": [
{
"dep1": 130,
"empid2": 370,
"sec": 2600
}
]
},
{
"maganer": [
{
"dep": 190,
"empid": 5850,
"sec": 190
}
]
}
]
}
]
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
//Part 1
Map<String ,Object> map1 = new HashMap<>();
map1.put("dep1", 130);
map1.put("empid2", 370);
map1.put("sec", 2600);
List<Map<String ,Object>> inventoriesList = new ArrayList<>();
inventoriesList.add(map1);
Map<String,List< Map<String ,Object> > > inventoriesMap = new HashMap<>();
inventoriesMap.put("match", inventoriesList);
//Part 2
Map<String ,Object> map2 = new HashMap<>();
map2.put("dep", 5850);
map2.put("empid", 190);
map2.put("sec", 190);
List<Map<String ,Object>> highPerformanceList = new ArrayList<>();
highPerformanceList.add(map2);
Map<String,List< Map<String ,Object> > > highPerformanceMap = new HashMap<>();
highPerformanceMap.put("overunder", highPerformanceList);
//Collect
List<Map<String, List<Map<String, Object>>>> ListMapTermMapList = new ArrayList< Map<String,List< Map<String ,Object> > > >();
ListMapTermMapList.add(inventoriesMap);
ListMapTermMapList.add(highPerformanceMap);
Map <String, List<Map<String,List<Map<String,Object>>>>> Od= new HashMap<>();
Odds.put("odds", ListMapTermMapList);
List<Map <String, List<Map<String,List<Map<String,Object>>>>>> oddsfinal= new ArrayList<Map <String, List<Map<String,List<Map<String,Object>>>>>>();
oddsfinal.add(Od);
//Format to Json
System.out.println(new Gson().toJson(oddsfinal));