Android:JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 // 输出结果为: // { // "function":"功能" // "text":11111, // "key":{"age":23,"name":"tom"}, // "user":[ // {"age":23,"name":"jem"}, // {"age":32,"jerry":"nay"} // ], // } private void btn2Click() //输出至服务端 { JSONObject jsonobj2 = new JSONObject(); JSONObject jsonobj1 = new JSONObject(); JSONArray jsonarr = new JSONArray(); try { jsonobj1.put( "function" , "功能" ); jsonobj1.put( "text" , 11111 ); jsonobj2.put( "name" , "tom" ); jsonobj2.put( "age" , 23 ); jsonobj1.put( "key" , jsonobj2); jsonarr.put( new JSONObject().put( "name" , "jem" ).put( "age" , 23 )); jsonarr.put( new JSONObject().put( "jerry" , "nay" ).put( "age" , 32 )); jsonobj1.put( "user" , jsonarr); String str = jsonobj1.toString(); textView.setText(str); Log.e( "d" , str); } catch (JSONException e) { e.printStackTrace(); } } private void btn1Click() //从服务端获取数据 { try { JSONObject jsonObject = new JSONObject(json); JSONObject jsonObject1 = jsonObject.getJSONObject( "category" ); String str = jsonObject1.getString( "@scheme" ); textView.setText(str); } catch (JSONException e) { e.printStackTrace(); } } String json = "{'category':{'@scheme':'http://www.douban.com/2007#kind','@term':'http://www.douban.com/2007#book'},'db:tag':[{'@count':111,'@name':'片山恭一'},{'@count':51,'@name':'日本'},{'@count':43,'@name':'日本文学'},{'@count':31,'@name':'满月之夜白鲸现'},{'@count':28,'@name':'小说'},{'@count':10,'@name':'爱情'},{'@count':7,'@name':'純愛'},{'@count':6,'@name':'外国文学'}],'title':{'$t':'满月之夜白鲸现'},'author':[{'name':{'$t':'[日] 片山恭一'}}],'summary':{'$t':'那一年,是听莫扎特、钓鲈鱼和家庭破裂的一年。说到家庭破裂,母亲怪自己当初没有找到好男人,父亲则认为当时是被狐狸精迷住了眼,失常的是母亲,但出问题的是父亲……。'},'link':[{'@rel':'self','@href':'http://api.douban.com/book/subject/1220562'},{'@rel':'alternate','@href':'http://book.douban.com/subject/1220562/'},{'@rel':'image','@href':'http://img3.douban.com/spic/s1747553.jpg'},{'@rel':'mobile','@href':'http://m.douban.com/book/subject/1220562/'}],'db:attribute':[{'$t':'7543632608','@name':'isbn10'},{'$t':'9787543632608','@name':'isbn13'},{'$t':'满月之夜白鲸现','@name':'title'},{'$t':'180','@name':'pages'},{'$t':'豫人','@name':'translator'},{'$t':'[日] 片山恭一','@name':'author'},{'$t':'15.00元','@name':'price'},{'$t':'青岛出版社','@name':'publisher'},{'$t':'平装','@name':'binding'},{'$t':'2005-1','@name':'pubdate'}],'id':{'$t':'http://api.douban.com/book/subject/1220562'},'gd:rating':{'@min':0,'@numRaters':289,'@average':'7.0','@max':10}}" ; 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1212834,如需转载请自行联系原作者