2016-10-17 5 views
0
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をreturn ID、return txtFile、return sentenceなどの戻り変数に変更したいと思います。どうやってするか?JSONの解析時にSystem.out.printlnをリターンとして置き換える方法は?

+0

あなたは '、通常それは蚊帳の外return'壊したら:更新

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(); } 

。これらの関連する変数をデータ構造体に格納し、ループが完了したときにそれを戻すことをお勧めします。 –

+0

私はそれを行う方法がわかりません。戻り変数を使用する目的は、ID、txtfileと文章を個別に表示したいのですが、リストに表示しません。 – HiPownedBi

+0

はい、私はそれを理解しています。各ループのデータ(つまりid、txtfile、sentence)をオブジェクトとしてラップし、リストに格納して返すことができます(投稿されたコードがメソッドであると仮定して)、反復表示されます。 –

答えて

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

私はZhi Kaiのコメントに同意します。 PS。私はまだコメントすることはできませんので、私は答えとしてこれを書いています。

POJOとデータ構造を作成します。あなたの場合、forループを使用しているので、JSONArrayから値のリストを返す必要があると仮定します。

あなたができることは次のとおりです。

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(); 
     } 
    } 
+0

リストの作成方法を指定できます HiPownedBi

+0

こんにちは私は自分の答えを更新しました。まず、クラス/モデルを作成する必要があります。私が作成したものはYourObjectという名前で、名前を変更します。次に、メソッドの戻り値の型をリストに設定します。 forループのすべての反復でわかるように、私はYourObjectの新しいインスタンスを作成し、値を設定してList に追加します – Anthony

関連する問題