NameValuePairで入れ子(nesting)になっているデータを表現する
HttpPostでデータサーバにPostするとき、データはNameValuePairを使うのが常套手段。
"foo"=>"bar", "hoge"=>"fuga"
というようなデータをPostしたいのであれば、
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2); nameValuePair.add(new BasicNameValuePair("foo", "bar")); nameValuePair.add(new BasicNameValuePair("hoge", "fuga"));
というように複数のデータをKey-Valueで渡せる事がわかる。
では、
"entry"=>{"title"=>"hello", "body"=>"hello world!"}
のように入れ子になったデータをPostする場合はどうしたらい良いのでしょうか?
実はKeyの所で文字列を、"[]"で区切ると階層構造扱いしてくれます。なので、上記のデータをPostするには以下のようにします。
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2); nameValuePair.add(new BasicNameValuePair("[entry][title]", "hello")); nameValuePair.add(new BasicNameValuePair("[entry][body]", "hello world!"));
以上です。