您好,我有一个JSF应用程序,它向API发出GET请求,获得一个JSON,然后从JSON获得一个元素,并用它填充JSF Datatable。我想用所有元素填充datatable,但只添加了一个元素。
这是我的Java代码:
@ManagedBean(name = "logic", eager = true)
@SessionScoped
public class Logic {
static JSONObject jsonObject = null;
static JSONObject jo = null;
static JSONArray cat = null;
private ArrayList<Logic> logics;
StringBuilder sb = new StringBuilder();
String cif2;
public void connect() {
try {
URL url = new URL("xxx");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while((inputLine = in.readLine())!= null){
System.out.println(inputLine);
sb.append(inputLine+"\n");
in.close();
}
} catch(Exception e) {System.out.println(e);}
}
private String cif;
public ArrayList<Logic> getLogics() {
return logics;
}
public Logic() throws ParseException {
connect();
JSONParser parser = new JSONParser();
jsonObject = (JSONObject) parser.parse(sb.toString());
cat = (JSONArray) jsonObject.get("mesaje");
for(int i = 0; i < cat.size(); i++) {
jo = (JSONObject) cat.get(i);
cif2 = jo.get("cif").toString();
logics = new ArrayList<Logic>(Arrays.asList(new Logic(cif2)));
}
}
public Logic(String cif) throws ParseException {
this.cif = cif;
}
public String getCif() {
return cif;
}
public void setCif(String cif) {
this.cif = cif;
}
}我写的插入代码是这样的:
public Logic() throws ParseException {
connect();
JSONParser parser = new JSONParser();
jsonObject = (JSONObject) parser.parse(sb.toString());
cat = (JSONArray) jsonObject.get("mesaje");
for(int i = 0; i < cat.size(); i++) {
jo = (JSONObject) cat.get(i);
cif2 = jo.get("cif").toString();
logics = new ArrayList<Logic>(Arrays.asList(new Logic(cif2)));
}
}它在json上执行for循环,但只将最后一个值相加。
我想要添加所有的值
提前感谢
发布于 2020-01-29 16:58:08
你应该用空的数组列表初始化logics。
private ArrayList<Logic> logics = new ArrayList<>();和
用logics.add(new Logic(cif2));替换logics = new ArrayList<Logic>(Arrays.asList(new Logic(cif2)));
发布于 2020-01-29 17:39:41
我想这句话才是罪魁祸首
logics = new ArrayList<Logic>(Arrays.asList(new Logic(cif2)));正如您所看到的,它将在每次循环迭代时创建逻辑的新实例,而不是将解析后的值添加/附加到列表中。您应该创建一个接受数据的变量,然后追加/添加类似以下内容的内容:
logics.append( new ArrayList<Logic>(Arrays.asList(new Logic(cif2))) );你在使用jackson-core-2.9.6吗?您可能希望使用ObjectMapper而不是JSONParser。
您可能想要将这几行代码
JSONParser parser = new JSONParser();
jsonObject = (JSONObject) parser.parse(sb.toString());
cat = (JSONArray) jsonObject.get("mesaje");
for(int i = 0; i < cat.size(); i++) {
jo = (JSONObject) cat.get(i);
cif2 = jo.get("cif").toString();
logics = new ArrayList<Logic>(Arrays.asList(new Logic(cif2)));
}至
ObjectMapper objectMapper = new ObjectMapper();//Create the ObjectMapper
// readValue accepts 2 param. string to be converted, and Object class/type to be used on mapping the data.
logics = objectMapper.readValue(sb.toString(), new TypeReference<List<Logic>>(){});
//If you have a adapter class to handle the entire JSON Record, you can use it to replace Logic.它的阅读和使用要简单得多。
PS:我注意到您正在从一个更大的JSON集合中检索数据。如果是这样的话,您应该有一个适配器/桥/帮助(无论您想叫它什么)类来接受/映射该JSON,而不是使用相同的类。在adapter类内部,您可以添加一个方法来提取数据、列表和其他希望从JSON记录中获取内容。
//Make sure you have correct attribute as to the JSON you are going to map in this object
class JSONReceived{
private . . . // attributes of the JSON
//getters and setters
public List<Mesaje> getMesajeList(){
//do your stuff here
}
}然后,您可以使用这些行,而不是前面的代码行。
ObjectMapper objectMapper = new ObjectMapper();//Create the ObjectMapper
JSONReceived json = objectMapper.readValue(sb.toString(), JSONReceived.class);
logics = json. getMesajeList();这样,它将帮助您发现记录大小中的错误和其他相关问题。
https://stackoverflow.com/questions/59962207
复制相似问题