【问题标题】:How to replace System.out.println as return when parsing JSON ?解析 JSON 时如何替换 System.out.println 作为返回?
【发布时间】:2017-02-25 22:06:32
【问题描述】:
 String jsonString = readJsonFile(filePath);

            try {
                JSONObject jsonObject = new JSONObject(jsonString);
         JSONArray result = jsonObject.getJSONArray("result");

         for (int i =0; i < result.length(); i++){
             JSONObject j = result.getJSONObject(i);
             String s = j.getString("sentence");
             int id = j.getInt("id");
             String txtFile = j.getString("txtfile");
             System.out.println("Sentence is:: " + s);
             System.out.println("Id is:: " + id);
             System.out.println("text file is:: " + txtFile);

         }
     } catch (JSONException e) {
         e.printStackTrace();
     }

目前,上面的代码可以打印出所有的记录。但是,我想把system.out.println改成返回变量,比如返回ID,返回txtFile,返回语句。该怎么做?

【问题讨论】:

  • 一旦你return,通常它会跳出循环。我建议您将这些相关变量存储在一个数据结构中,并在循环完成时返回它。
  • 我不知道该怎么做。使用返回变量的目的,我想将id、txtfile和句子分开显示,而不是在列表中显示。
  • 是的,我明白这一点。您可能希望参考答案,将每个循环的数据(即 id、txtfile、句子)包装为对象,将其存储在列表中并返回(假设您发布的代码是一种方法)以对其进行迭代和显示。

标签: java arrays json for-loop return


【解决方案1】:

我同意智凯在评论中所说的。 PS。我还不能发表评论,所以我写这个作为答案。

创建一个 POJO 并使用数据结构。在您的情况下,您使用的是 for 循环,因此我假设您需要从 JSONArray 返回值列表。

这是你可以做的。

String jsonString = readJsonFile(filePath);

        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray result = jsonObject.getJSONArray("result");

            List<YourObject> yourObjectToReturn = new ArrayList<YourObject>();

            for (int i = 0; i < result.length(); i++) {
                YourObject yourObject = new YourObject();
                JSONObject j = result.getJSONObject(i);
                String s = j.getString("sentence");
                int id = j.getInt("id");
                String txtFile = j.getString("txtfile");
                yourObject.setId(id);
                yourObject.setTxtFile(txtFile);
                yourObject.setSentence(s);
                yourObjectToReturn.add(yourObject);

            }
            return yourObjectToReturn;
        } catch (JSONException e) {
            e.printStackTrace();
        }

更新:

public class YourObject {

    private String id;
    private String txtFile;
    private String sentence;

    public int getId() {
         return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTxtFile() {
        return txtFile;
    }
    public void setTxtFile(String txtFile) {
        this.txtFile = txtFile;
    }
    public String getSentence() {
        return sentence;
    }
    public void setSentence(String sentence) {
        this.sentence = sentence;
    }
}

public List<YourObject> returnObject(){
        String jsonString = readJsonFile(filePath);

        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray result = jsonObject.getJSONArray("result");

            List<YourObject> yourObjectToReturn = new ArrayList<YourObject>();

            for (int i = 0; i < result.length(); i++) {
                YourObject yourObject = new YourObject();
                JSONObject j = result.getJSONObject(i);
                String s = j.getString("sentence");
                int id = j.getInt("id");
                String txtFile = j.getString("txtfile");
                yourObject.setId(id);
                yourObject.setTxtFile(txtFile);
                yourObject.setSentence(s);
                yourObjectToReturn.add(yourObject);

            }
            return yourObjectToReturn;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

【讨论】:

  • 可以指定如何创建 List
  • 您好,我更新了我的答案。您需要做的是首先创建一个类/模型。我创建的一个名为 YourObject,只需重命名它。然后在您的方法上将返回类型设置为 List。然后,正如您在 for 循环的每次迭代中看到的那样,我创建了一个 YourObject 的新实例并设置了值,然后将其添加到 List
【解决方案2】:

创建一个对象。使用 arraylist 来存储您的对象并在以后使用它。

public class myItem{
   String sentence;
   int id;
   String txtfile;

   public myItem(){
   }

   public String getSentence(){
       return sentence;
   }
   public setSentence(String s){
      this.sentence = sentence;
   }
}


public void yourFunction(){
   try {
       ArrayList <myItem> myList = new ArrayList();

     JSONObject jsonObject = new JSONObject(jsonString);
     JSONArray result = jsonObject.getJSONArray("result");

     for (int i =0; i < result.length(); i++){
         JSONObject j = result.getJSONObject(i);
         String s = j.getString("sentence");

         myItem newItem = new myItem();
         newItem.setSentence(s);

         myList.add(newItem);

     }
 } catch (JSONException e) {
     e.printStackTrace();
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2013-04-05
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多