-2

I have the following code:

import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;

inputParams="custObj237Id,1001,custObjNm,nome1,custObjDesc,desc1,statusCd,status1,periodicity,peridiocity1,periods,period1";

String[] arrayParams = inputParams.split(",");  
List<String> listParams = new ArrayList<String>();
List<String> key        = new ArrayList<String>();
List<String> value      = new ArrayList<String>();

for(String s : arrayParams) {
    if(s != null && s.length() > 0) {
        listParams.add(s);
    }
}
        
for(int i=0;i<listParams.size;i++) {
    if (i % 2 == 0){
        key.add(listParams.get(i));
    }else{ 
        value.add(listParams.get(i));
    }
} 
     
listParams.clear();     
     
int c1 = 0, c2 = 0;
    
while(c1 < key.size() || c2 < value.size()) {
   if(c1 < key.size()){
       listParams.add((String) key.get(c1++));
   }
   if(c2 < value.size()){
       listParams.add((String) value.get(c2++));
   }
}

code return:

[custObj237Id, 1001, custObjNm, nome1, custObjDesc, desc1, statusCd, status1, periodicity, peridiocidade1, periods, periodo1]

How I can convert ArrayList to JSONObject without using GSON?

[custObj237Id, 1001, custObjNm, nome1, custObjDesc, desc1, statusCd, status1, periodicity, peridiocidade1, periods, periodo1]

in 

{"custObj237Id":1001,"custObjNm":nome1,"custObjDesc":desc1,"statusCd":status1,"periodicity":periodicity1,"periods":periodo1}

  • use Jackson -> https://stackoverflow.com/questions/40967921/create-json-object-using-jackson-in-java – fantaghirocco Jan 07 '21 at 00:07
  • Your code is not using Gson right now, so why did you feel the need to say "without using GSON"? – Andreas Jan 07 '21 at 00:59
  • `"custObjNm":nome1` is not valid JSON. Why do you want invalid JSON? Why would you even expect a JSON library to be able to do it? – Andreas Jan 07 '21 at 01:00
  • @Andreas, because in the examples I saw, GSON was used to convert the ArrayList to JSONObject and, I tried to use it, but I found many errors, so I decided to look for another way to do it. As for the invalid format, sorry, I don't have so much experience using JSON, I just needed that the format {"key": "value"} was applied to my return, because it is a prerequisite for the execution of another action – Losantosw Jan 07 '21 at 01:23

2 Answers2

0

convert ArrayList to JSONObject without using GSON you can use

JSONObject EdgeJsonObjsub = new JSONObject();
JSONArray EdgeJsonArrayMain=new JSONArray();

sample code is

public static void main(String[] args) 
    {
    List<Integer> y = new ArrayList<Integer>();
    y.add(100);
    y.add(200);
     JSONObject EdgeJsonObjsub = new JSONObject();
     JSONArray EdgeJsonArrayMain=new JSONArray();
     for(int i=0; i<y.size(); i++)
     {
         EdgeJsonArrayMain.put(y.get(i));
     }
     EdgeJsonObjsub.put("12", EdgeJsonArrayMain);
     System.out.println(EdgeJsonObjsub);
    
    }

output is

{"12":[100,200]}
Gomathy M
  • 338
  • 2
  • 10
0

I managed to solve the problem presented. To do this, I created the format I needed on the ArrayList itself, converted it to a String and inserted what was missing, to create a JSONObject in the format of a String:

old

while(c1 < key.size() || c2 < value.size()) {
   if(c1 < key.size()){
       listParams.add((String) key.get(c1++));
   }
   if(c2 < value.size()){
       listParams.add((String) value.get(c2++));
   }
}

new

while(c1 < key.size() || c2 < value.size()) {
    if(c1 < key.size()){
        listParams.add("\"" + (String) key.get(c1++) + "\":");
    }
    if(c2 < value.size()){
        listParams.add("\"" + (String) value.get(c2++) + "\"");
    }
}
      
String res = "";
      
res += "{";
res += String.join("", listParams);
res += "}"; 
     
for(int j=0;j<res.length();j++){         
   if(res[j] == "\"" && res[j+1] == "\""){
      res = res.replaceAll("\"\"","\",\"");
   } 
}

return res:

{"custObj237Id":"1001","custObjNm":"nome1","custObjDesc":"desc1","statusCd":"status1","periodicity":"peridiocidade1","periods":"periodo1"}

At this point, there is already JsonObject, but it is like a String, so to convert just use the code below:

JSONObject jsonObject = new JSONObject(res);

After the conversion to JSONObject if you search for a .get("key"), you will find the data.

The resolution is a bit extensive, but it is an alternative for those who have problems using GSON. Although it is not a "conventional" JSON, in some cases it is necessary to do it this way.

I hope I have helped.