Android でhttps://code.google.com/p/google-gson/を使用し、PHP で json_encode() を使用するのは非常に簡単です (blackpanthers の応答も参照してください)。具体的なサンプル:
最初に PHP で json_encode() を実装し、ブラウザーで呼び出して、結果を確認できるようにします。たとえば、次のような結果が得られます。
{"user_name":"my name", "user_surname":"my_surname", "phones": { "mobile":"11111" }}
これで、プロパティ "user_name"、"user_surname"、および "phones" を含む JSON エンティティが得られました。一方、「phones」はネストされたエンティティであり、プロパティ「mobile」が含まれています。
ここで、エンティティごとに Java クラスを作成するため、2 つ必要です。1 つは「電話」を含み、もう 1 つはエンティティ「電話」を含むすべてのプロパティを含みます。
class Phones {
// with this annotation you can bind to
// properties from JSON named differently than
// the property in this class
@SerializedName("mobile")
String thePhone;
}
class MyJson {
String user_name;
String user_surname;
Phones phones;
}
まあ、それだけです:)ああ、わかりました、最後の部分
...
InputStream is = new URL("http://www.my.php.returning.json").openStream();
InputStreamReader isr = new InputStreamReader();
MyJson myJson = new Gson().fromJson(isr , MyJson.class);
... //close stream, handle exceptions, etc.
// now you've got that all in the myJson object...
どうぞ!