2016-04-20 7 views
2

私は、ユーザーからデータを取得するAndroidフォームを使用しています。私はすべてのデータをバンドルしています。しかし、バンドルをJSONObjectsに保存する方法については助けが必要です。以下のコードは、BundleAndroidフォームデータをJSONObjectsに変換

Fragment data = new Fragment(); 
    Bundle itembundle =new Bundle();   
    itembundle.putString("title",title.getText().toString()); 
    itembundle.putString("subject",subject.getText().toString()); 
    itembundle.putString("content", content.getText().toString()); 
    itembundle.putString("source", source.getText().toString()); 
    if (title.getText()!=null && subject.getText() != null&&  content.getText()!= null && source.getText()!= null){ 
     data.setArguments(itembundle); 
     title.setText(""); 
     subject.setText(""); 
     content.setText(""); 
     source.setText(""); 
    } else{ 
      //code to throw exception of an empty form element 
    } 

へのデータ格納、それを取得し、以下のコードは、バンドルからデータを取得します。

public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Bundle getData = this.getArguments(); 
    if (getArguments()!=null){ 
     item.setItemTitle((String) getData.getCharSequence("title")); 
     item.setItemContent((String) getData.getCharSequence("subject")); 
     item.setItemDescription((String) getData.getCharSequence("content")); 
     itemList.add(item); 
    } 

} 

JSONObjectsにこれを入れる方法を教えてください。ありがとう。

+0

実際に何が必要ですか? BundleのJSONObjectインスタンスを使用するか、両方を使用したいと思います。 –

+0

私は本当にAndroidが初めてです。だから最初にバンドルを使い始めました。ですから、私はJSONObjectをウェブに送るためにそれを使う方法を知りたいのです。 – Jeremiah

+0

@Jeremiah Webに送信したい場合は、私の回答に表示されているようにJSONObjectを作成し、それをStringに変換してWebに送ることができます。 –

答えて

1

これらの値を使用してJSONObjectを作成することを意味しますか? あなたはこれを試していました:これだけ

JSONObject info = new JSONObject(); 
try{ 
    info.put("title",getData.getCharSequence("title")); 
    info.put("subject",getData.getCharSequence("subject"));); 
}catch(JSONException e){ 
    e.printStackTrace(); 
} 

を、あなたは今JSONObjectを持っています。あなたはこのようないくつかの操作を行う必要があり、リスト内の各項目について

JSONObject info1 = new JSONObject(); 
info1.put("firstName","newFirstName"); 
info1.put("lastName","newLastName"); 

JSONObject info2 = new JSONObject(); 
info2.put("firstName","newFirstName"); 
info2.put("lastName","newLastName"); 

JSONArray myArray = new JSONArray(); 
myArray.put(info1); 
myArray.put(info2); 

JSONObject newObj = JSONObject(); 
newObj.put(myArray); 
+0

私は実際にはpublicメソッド 'info.add()'を理解していません、それはJSONObjectsのパブリックメソッドですか? – Jeremiah

+0

申し訳ありませんが、私は 'ArrayList'を使いこなしました。 JSONには 'put'メソッドがあります。私は私のandwerを編集しました –

2

:あなたはまた、このような情報の配列を作成することができます

JSONObject jsonObj = new JSONObject(); 
try { 
    jsonObj.put("title", item.getItemTitle()); 
    jsonObj.put("content", item.getItemContent()); 
    jsonObj.put("description", item.getItemDescription()); 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

もう一つの方法は、gsonを使用することです、アイテムオブジェクトを指定すると、電話するだけです

String jsonString = gson.toJson(item) 

ここではGsonの開発者ガイドです https://sites.google.com/site/gson/gson-user-guide

1

あなたは..あなたがあなたのlogcatでjsonStringを見ることができる今、この

JSONObject jsonObject = new JSONObject(); 
try{ 

jsonObject.put("key","value"); 
........ 
//for your case it would be like this 
jsonObject.put("title",title.getText().toString()); 


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

String jsonString = jsonObject.toString(); 
Log.e("json",jsonString); 

を試してみてくださいその簡単な新鮮JSONObjectについてこの

try 
    { 

     JSONObjects json =new JSONObjects(); 
     json.put(Key,value); 
     json.put(Key,value); 
     //and pass the data 
     Bundle itembundle =new Bundle(); 
     itembundle.putString("items",json.toString()); 


    //and get data 

    JSONObjects json=new JSONObjects(getArguments().getString("items")); 
    String title=json.getString(Key); 



    }catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
1

を試すことができます。

関連する問題