Java 列表仅用JSON中的一行填充,应该用许多行填充
您好,我有一个JSF应用程序,它向API发出GET请求,获取JSON,JSON从中获取元素并用它填充JSF数据表。我想用所有元素填充datatable,但只添加一个元素 这是我的Java代码:Java 列表仅用JSON中的一行填充,应该用许多行填充,java,json,Java,Json,您好,我有一个JSF应用程序,它向API发出GET请求,获取JSON,JSON从中获取元素并用它填充JSF数据表。我想用所有元素填充datatable,但只添加一个元素 这是我的Java代码: @ManagedBean(name = "logic", eager = true) @SessionScoped public class Logic { static JSONObject jsonObject = null; static JSONObject jo = null
@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循环,但只添加最后一个值
我想添加所有值
提前感谢您应该使用空的arraylist初始化逻辑
private ArrayList<Logic> logics = new ArrayList<>();
及
Replace logics=new ArrayListStarRays.asListnew Logiccif2;使用logics.addnew Logiccif2 我认为这是罪魁祸首
logics = new ArrayList<Logic>(Arrays.asList(new Logic(cif2)));
到
这样,它将帮助您发现记录大小中的错误和其他相关问题。JSF数据表从不从json填充,而是从java列表填充。因此,如果您的循环未能添加全部,那么这是一个纯java/json的东西,与JSF无关。调用api也与JSF无关。所有这些都是纯java代码。好的,谢谢你的回答,但是你能告诉我我的代码有什么问题吗?我从JSON中获取数据,并输入一个变量,我试图将其加载到java列表中,但它只加载一个值。。。
logics.append( new ArrayList<Logic>(Arrays.asList(new Logic(cif2))) );
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.
//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();