How to Split JSONArray
Hi All,
I have customized CF model, as shown below.

NOTE - My location Name value is coming from database
I have written the JSON and in JSON my location name is coming like below.
************* JSON **************
"testLocations":
{"City":[{"id":"161341","name":"Badarganj"}],
"Country":[{"id":"157031","name":"India"}]}
I have written below java to get my testlocations JSON
************** JAVA Needs To Update*************
private void settestLocations(Resource masterResource, Node masterNode,
testPublishModel testModel) throws RepositoryException {
LOGGER.info("Inside locations");
JsonObject testLocations = new JsonObject();
ValueMap masterResourceValueMap = masterResource.getValueMap();
if(masterNode.hasProperty(Constants.test_LOCATIONS)) {
String[] testLocationsStringArray = masterResourceValueMap.get(Constants.test_LOCATIONS, String[].class);
for (String testLocationString : testLocationsStringArray) {
JsonObject testLocation = gson.fromJson(testLocationString, JsonObject.class);
String locationType = StringUtils.EMPTY;
JsonArray locationName = new JsonArray();
if (testLocation.has(LOCATION_TYPE)) {
locationType = testLocation.get(LOCATION_TYPE).getAsString();
}
if (testLocation.has(LOCATION_NAME)) {
locationName = testLocation.get(LOCATION_NAME).getAsJsonArray();
LOGGER.info("locationName12345 ==== " + locationName);
}
testLocations.add(locationType, locationName);
}
}
testModel.settestLocations(testLocations);
}
********************* testmodel JAVA class is as below ********************
import java.util.List;
import com.google.gson.JsonObject;
public class TestModel {
JsonObject testLocations;
List<testLocationsEntity> locationName;
public JsonObject gettestLocations() {
return testLocations;
}
public void settestLocations(JsonObject testLocations) {
this.testLocations = testLocations;
}
public List<testLocationsEntity> gettestLocationName() {
return locationName;
}
public void settestLocationName(List<testLocationsEntity> testLocationNameList) {
this.locationName = testLocationNameList;
}
@Override
public String toString() {
return "TestModel [ testLocations=" + testLocations
+ ", locationName123=" + locationName + "]";
}
}
Here requirement is that my JSON should come like below , as highlighted in Red
"testLocations":{
"City":[{"id":"161341","testName[Badarganj]"}],
"Country":[{"id":"161341","testName[India]"}]
}
I tried lots of option but still not able to achieve the required JSON .
Thanks in advance
Subrat