【发布时间】:2019-11-18 15:43:53
【问题描述】:
String input = "Vish,Path,123456789";
预期输出为 Json 字符串,并且线程安全 = {"name":"Vish","surname":"Path","mobile":"123456789"}
我尝试过使用
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
但每次我创建新对象时 -
MappingObject[] studentArray = new MappingObject[1];
studentArray[0] = new MappingObject("Vish","Path","123456789");
我使用 split() 分隔了这个逗号分隔的字符串
System.out.println("JSON "+gson.toJson(studentArray));
【问题讨论】:
-
你遇到了什么问题?
-
@sidgate 我尝试使用 GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create();但每次我创建新对象时 - MappingObject[] studentArray = new MappingObject[1]; studentArray[0] = new MappingObject("Vish","Path","123456789");我使用 split() System.out.println("JSON "+gson.toJson(studentArray)); 分隔了这个逗号分隔的字符串。因此,即使我与多个学生一起工作,我也只想创建单个对象,并且转换过程也应该是线程安全的。
-
使用带有 Map
作为参数的 JSONObject 构造函数