在Java中向列表添加字符串
请看下面的代码片段。 我看了stackoverflow上提供的一些解决方案,用于将字符串添加到列表中。 在下面的例子中,它们的效果不好在Java中向列表添加字符串,java,Java,请看下面的代码片段。 我看了stackoverflow上提供的一些解决方案,用于将字符串添加到列表中。 在下面的例子中,它们的效果不好 @RequestMapping(value = "/rest/EmployeeDept/", method = RequestMethod.GET) // ResponseEntity is meant to represent the entire HTTP response public ResponseEntity<EmployeeDeptRespo
@RequestMapping(value = "/rest/EmployeeDept/", method = RequestMethod.GET)
// ResponseEntity is meant to represent the entire HTTP response
public ResponseEntity<EmployeeDeptResponse> getDept()
{
EmployeeDeptResponse deptResponse = new EmployeeDeptResponse();
HttpStatus httpStatus;
List<EmployeeDept> employeeDeptList = new ArrayList<EmployeeDept>();
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(
"http://localhost:8082/rest/EmployeeDept/");
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
employeeDeptList.add(output);
}
deptResponse.setItems(employeeDeptList);
httpClient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpStatus = HttpStatus.OK;
return new ResponseEntity<EmployeeDeptResponse>(deptResponse,httpStatus);
}
}
在上面的代码中,我有一个列表“employeeDeptList”和一个字符串“Output”。
我需要将此字符串添加到列表中
你们谁能提供合适的建议
提前感谢。
员工接受列表
的类型为数组列表
因此,当您执行employeeDeptList.add(输出)时
,您正试图将字符串添加到员工接受列表
,而该字符串应为员工接受列表
因此,您要么将输出
设置为员工接受
,要么重新考虑如何使用它
作为建议,我将假设您的输出应包含创建EmployeeDept所需的信息。您可能希望解析该信息并创建一个EmployeeDept dept=newemployeedept(parsedId,parsedDept)
然后将其添加到employeeDeptList
asemployeeDeptList.add(dept)代码>EmployeeDept列表是EmployeeDept对象的列表。您正在尝试向EmployeeDept列表中添加字符串。这是不可能的,除非您将输出变量的类型更改为EmployeeDept。代码中有问题
while ((output = br.readLine()) != null) {
employeeDeptList.add(output);
}
输出
是一个字符串,您正试图将其添加到列表
。你不能那样做。如果要将output
添加到列表中,则应创建字符串列表。类似于List
正如你提到的,你得到的是
{
"1499921014230": {
“id”:1499921014230,
“部门”:“机械师”
},
"1499921019747": {
“id”:1499921019747,
“部门”:“民事”
}
}
如果你能改变它,你可以试着把它变成一个简单的对象数组
[
{
“id”:1499921014230,
“部门”:“机械师”
},
{
“id”:1499921019747,
“部门”:“民事”
}
]
如果您使用maven,请在下面添加依赖项,或者只将.jar添加到库中
org.json
json
20090211
那就试试这个,
while ((output = br.readLine()) != null) {
JSONArray jsonArr = new JSONArray(output);
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
String dept = jsonObj.getString("dept");
int id = jsonObj.getInt("id");
System.out.println("id : " + id + " dept : " + dept);
employeeDeptList.add(new EmployeeDept(id, dept));
}
}
while((output=br.readLine())!=null){
JSONArray jsonArr=新的JSONArray(输出);
for(int i=0;i
如果响应是有效的json(指定的头),为什么不尝试将其映射到对象
ObjectMapper mapper = new ObjectMapper();
//assuming your response entity content is a list of objects (json array, since you specified header 'application/json'
String jsonArray = String theString = IOUtils.toString(response.getEntity().getContent(), encoding);
employeeDeptList = List<Employee> list = mapper.readValue(jsonString, TypeFactory.defaultInstance().constructCollectionType(List.class, employeeDeptList.class));
//assuming your response is a single object
String json = String theString = IOUtils.toString(response.getEntity().getContent(), encoding);
employeeDeptList.add(mapper.readValue(json, Employee.class));
//assuming every line of content is an object (does not really make sense)
BufferedReader br = new BufferedReader(ew InputStreamReader((response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
employeeDeptList.add(mapper.readValue(output, Employee.class));
}
ObjectMapper mapper=new ObjectMapper();
//假设您的响应实体内容是对象列表(json数组,因为您指定了标题“application/json”
String jsonArray=String theString=IOUtils.toString(response.getEntity().getContent(),encoding);
employeeDeptList=List List=mapper.readValue(jsonString,TypeFactory.defaultInstance().constructCollectionType(List.class,employeeDeptList.class));
//假设您的响应是单个对象
String json=String theString=IOUtils.toString(response.getEntity().getContent(),编码);
add(mapper.readValue(json,Employee.class));
//假设每一行内容都是一个对象(没有真正意义)
BufferedReader br=新的BufferedReader(ew InputStreamReader((response.getEntity().getContent()));
字符串输出;
而((output=br.readLine())!=null){
add(mapper.readValue(output,Employee.class));
}
无法将字符串添加到列表中。
是。但是如何解决此问题?您正在从输入流JSON对象中读取的行是吗?如果是,您需要使用JSON解析器将它们从字符串转换为EmployeeDept
。此转换不会自动进行。您必须告诉计算机这是错误的这就是你想做的,把它放进你的程序中。它无法读懂你的心思。@KaustubhLonkar你能按照ajb
的注释来做吗。这就是你需要做的。谢谢,但那不起作用。我需要采取一些其他方法。你完全正确。问题在于代码。我需要调整它。我尝试将输出转换为列表,但没有成功工作。早些时候,我有其他的东西。我指的是在终端和浏览器中给我以下输出的不同代码。{“149991014230”:{“id”:149991014230,“dept”:“mechanics”},“149991019747”:{“id”:149991019747,“dept”:“civil”}使用信息(id
和dept
)您必须创建EmployeeDept
对象。并将该对象添加到employeeDeptList
中。您也这样做了。但它不起作用。还尝试了强制转换为list。
while ((output = br.readLine()) != null) {
employeeDeptList.add(output);
}
while ((output = br.readLine()) != null) {
JSONArray jsonArr = new JSONArray(output);
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
String dept = jsonObj.getString("dept");
int id = jsonObj.getInt("id");
System.out.println("id : " + id + " dept : " + dept);
employeeDeptList.add(new EmployeeDept(id, dept));
}
}
ObjectMapper mapper = new ObjectMapper();
//assuming your response entity content is a list of objects (json array, since you specified header 'application/json'
String jsonArray = String theString = IOUtils.toString(response.getEntity().getContent(), encoding);
employeeDeptList = List<Employee> list = mapper.readValue(jsonString, TypeFactory.defaultInstance().constructCollectionType(List.class, employeeDeptList.class));
//assuming your response is a single object
String json = String theString = IOUtils.toString(response.getEntity().getContent(), encoding);
employeeDeptList.add(mapper.readValue(json, Employee.class));
//assuming every line of content is an object (does not really make sense)
BufferedReader br = new BufferedReader(ew InputStreamReader((response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
employeeDeptList.add(mapper.readValue(output, Employee.class));
}