Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换ArrayList<;字符串>;不使用GSON连接到JSONObject_Java_Json_List_Arraylist - Fatal编程技术网

Java 转换ArrayList<;字符串>;不使用GSON连接到JSONObject

Java 转换ArrayList<;字符串>;不使用GSON连接到JSONObject,java,json,list,arraylist,Java,Json,List,Arraylist,我有以下代码: 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 = inp

我有以下代码:

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++));
   }
}

[custObj237Id, 1001, custObjNm, nome1, custObjDesc, desc1, statusCd, status1, periodicity, peridiocidade1, periods, periodo1]
{"custObj237Id":"1001","custObjNm":"nome1","custObjDesc":"desc1","statusCd":"status1","periodicity":"peridiocidade1","periods":"periodo1"}
如何在不使用GSON的情况下将ArrayList转换为JSONObject?

[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}


不使用GSON将ArrayList转换为JSONObject 你可以用

JSONObject EdgeJsonObjsub = new JSONObject();
JSONArray EdgeJsonArrayMain=new JSONArray();
示例代码是

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);
    
    }

我设法解决了提出的问题。为此,我在ArrayList本身上创建了所需的格式,将其转换为字符串并插入缺少的内容,以创建字符串格式的JSONObject:

旧的

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++));
   }
}

此时,已经有了JsonObject,但它就像一个字符串,因此要转换,只需使用以下代码:

JSONObject jsonObject = new JSONObject(res);
转换为JSONObject后,如果搜索.get(“key”),您将找到数据

该解决方案有点广泛,但对于那些使用GSON有问题的人来说,它是一种替代方案。虽然它不是一个“传统”的JSON,但在某些情况下有必要这样做


我希望我能提供帮助。

使用Jackson->您的代码现在没有使用Gson,那么为什么您觉得有必要说“不使用Gson”?
“custObjNm”:nome1
不是有效的JSON。为什么要使用无效的JSON?为什么你会期望JSON库能够做到这一点?@Andreas,因为在我看到的示例中,GSON被用于将ArrayList转换为JSONObject,我尝试使用它,但发现了许多错误,所以我决定寻找另一种方法。至于无效的格式,对不起,我没有那么多使用JSON的经验,我只需要将格式{“key”:“value”}应用于我的返回,因为它是执行另一个操作的先决条件
{"custObj237Id":"1001","custObjNm":"nome1","custObjDesc":"desc1","statusCd":"status1","periodicity":"peridiocidade1","periods":"periodo1"}
JSONObject jsonObject = new JSONObject(res);