I have to read a sample pom file and write all technology and version in to json file, Im able to get the output in this format:
["{ name:junit ,Version:4.12}","{ name:spring-batch-test ,Version:3.0}","{ name:spring-boot-starter }","{ name:slf4j-api }"]
However I want to get output in this format:
[{ "name":"junit" ,"Version":"4.12"},{" name":"spring-batch-test" ,"Version":"3.0"},{"name":"spring-boot-starter" }]
My code :
Map<String, String> dependencies = Maps.newHashMap();
dependencies = populateProjectDepedencies(dependencies, pomFile);
In populateProjectDependencies
for (Dependency dependency : dependencyList) {
String version = "0.0";
if (dependency.getVersion() != null &&
dependency.getVersion().startsWith("${"))
{
version = (String) properties.get(dependency.getVersion()
.substring(2, dependency.getVersion().length() - 1));
} else {
version = dependency.getVersion();
}
if (version != null) {
String a1[]=version.split("\\.");
int i=a1.length;
if(i>=2)
{
version=a1[0]+"."+a1[1];
}
dependencies.put("{name:"+dependency.getArtifactId(),",
Version:"+version+"}" );
JSONArray jsonArray = prepareJsonObject(dependencies);
genarateTechnologyRadarJson(jsonArray);
writer.write(jsonArray.toJSONString());
答案 0 :(得分:0)
Because, you are adding the value as a String
"{ name:"+dependency.getArtifactId(),"
Even, I'm not sure why are you manually constructing the JSON instead just pass the Map
object to JSONObject
.
JSONObject obj=new JSONObject(yourmap);
答案 1 :(得分:0)
As I understand from your question, you are holding json as String
array but you want to hold data as JSONObject
array. So,
JSONArray ja = new JSONArray();
for (Dependency dependency : dependencyList) {
.....
JSONObject obj=new JSONObject();
obj.put("name",dependency.getArtifactId());
obj.put("Version",version);
ja.put(obj);
//remove dependencies.put,JSONArray. and genarateTechnologyRadarJson(jsonArray);
}
writer.write(ja.toString());
UPDATE
This should be your complete code
JSONArray jsonArray = new JSONArray();
for (Dependency dependency: dependencyList) {
String version = "0.0";
if (dependency.getVersion() != null &&
dependency.getVersion().startsWith("${")) {
version = (String) properties.get(dependency.getVersion()
.substring(2, dependency.getVersion().length() - 1));
} else {
version = dependency.getVersion();
}
if (version != null) {
String a1[] = version.split("\\.");
int i = a1.length;
if (i >= 2) {
version = a1[0] + "." + a1[1];
}
}
JSONObject obj=new JSONObject();
obj.put("name",dependency.getArtifactId());
obj.put("Version",version);
jsonArray.put(obj);
}
writer.write(jsonArray.toJSONString());