解析JSON时如何将System.out.println替换为return?

时间:2016-10-17 02:24:30

标签: java arrays json for-loop return

 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,返回句子。怎么做?

2 个答案:

答案 0 :(得分:1)

创建一个对象。使用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();
 }
}

答案 1 :(得分: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();
        }
    }