服务器 远程存储,远程服务器存储之JSON(示例代码)

1 packagecom.hanqi.testapp3;2

3 importandroid.support.v7.app.AppCompatActivity;4 importandroid.os.Bundle;5 importandroid.view.View;6 importandroid.widget.EditText;7

8 importcom.google.gson.Gson;9 importcom.google.gson.reflect.TypeToken;10

11 importorg.json.JSONArray;12 importorg.json.JSONException;13 importorg.json.JSONObject;14

15 importjava.util.ArrayList;16 importjava.util.List;17 importjava.util.Map;18

19 public class TestActivity4 extendsAppCompatActivity {20

21 EditText et_3;22

23 @Override24 protected voidonCreate(Bundle savedInstanceState) {25 super.onCreate(savedInstanceState);26 setContentView(R.layout.activity_test4);27

28 et_3=(EditText)findViewById(R.id.et_3);29

30 }31

32 //原生从Json字符串转成对象

33 public voidbt1_OnClick(View v)34 {35

36 String strJson="{\\"id\\":1,\\"name\\":\\"大虾\\", " +

37 "\\"price\\":12.3, " +

38 "\\"imagePath\\":\\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\\"}";39

40 //从Json字符串转成对象

41 try{42 JSONObject jo = newJSONObject(strJson);43

44 int id=jo.getInt("id");45 String name=jo.getString("name");46 double price=jo.getDouble("price");47 String imagePath=jo.getString("imagePath");48

49 ShopInfo shopInfo=newShopInfo(id,imagePath,name,price);50

51 et_3.setText(shopInfo.toString());52

53

54 }55 catch(Exception e)56 {57 e.printStackTrace();58 }59

60

61 }62

63 //Gson转对象

64 public voidbt2_OnClick(View v)65 {66 //转对象

67 String jsonstr="{\\"id\\":3,\\"name\\":\\"大虾\\"," +

68 "\\"price\\":12.3," +

69 "\\"imagePath\\":\\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\\"}";70

71 ShopInfo shopInfo=new Gson().fromJson(jsonstr,ShopInfo.class);72

73 et_3.setText(shopInfo.toString());74

75 // //转集合76 //String jsonstr1="[{\\"id\\":3, \\"name\\":\\"大虾1\\", \\"price\\":12.3, " +77 //"\\"imagePath\\":\\"http://192.168.10.165:8080/f1.jpg\\"}," +78 //"{\\"id\\":4, \\"name\\":\\"大虾2\\", \\"price\\":12.5, " +79 //"\\"imagePath\\":\\"http://192.168.10.165:8080/f2.jpg\\"}]";80 //

81 //List list=new Gson().fromJson(jsonstr1, new TypeToken>() {82 //}.getType());83 //

84 //et_3.setText(list.toString());85 //

86 // //对象转JSON87 //ShopInfo info=new ShopInfo(3,"http://www.sina.com","KK",1000);88 //

89 //String json=new Gson().toJson(info);90 //

91 //et_3.setText(json);92 //

93 // //集合转JSON94 //List list1=new ArrayList();95 //

96 //list1.add(new ShopInfo(3, "http://www.sina.com","KK", 1000));97 //list1.add(new ShopInfo(4, "http://www.sina.com/cn", "KK1", 2000));98 //String json1=new Gson().toJson(list1);99 //

100 //et_3.setText(json1);101

102 //String jsonstr2="{\\"my name\\":\\"大虾\\",\\"1\\":12}";103 //

104 //Map map=new Gson().fromJson(jsonstr2,new TypeToken>(){}.getType());105 //

106 //et_3.setText(map.toString());

107

108 }109

110 //原生从Json字符串转成集合

111 public voidbt3_OnClick(View v)112 {113

114 //转集合

115 String jsonstr="[{\\"id\\":1,\\"name\\":\\"大虾1\\",\\"price\\":12.3," +

116 " \\"imagePath\\":\\"http://192.168.10.165:8080/f1.jpg\\"}," +

117 "{\\"id\\":2, \\"name\\":\\"大虾2\\", \\"price\\":12.5, " +

118 "\\"imagePath\\":\\"http://192.168.10.165:8080/f2.jpg\\"}]";119

120 //从Json字符串转成集合

121 try{122

123 List list=new ArrayList();124

125 //1.将json字符串包装JSONArray对象

126 JSONArray jsonArray=newJSONArray(jsonstr);127

128 //2.遍历JSONArray对象所有元素(JSONObject),并将每个元素封装为shopInfo,并添加到list

129 for (int i=0;i

133 //从对象中根据key得到相应的value

134 int id=jsonObject.getInt("id");135 String name=jsonObject.getString("name");136 double price=jsonObject.getDouble("price");137 String imagePath=jsonObject.getString("imagePath");138

139 //封装ShopInfo对象

140 ShopInfo shopInfo=newShopInfo(id,imagePath,name,price);141

142 list.add(shopInfo);143

144 }145

146 et_3.setText(list.toString());147

148

149 }150 catch(Exception e)151 {152 e.printStackTrace();153 }154

155

156 }157

158

159 //Gson转集合

160 public voidbt4_OnClick(View v)161 {162 //转集合

163 String jsonstr="[{\\"id\\":3, \\"name\\":\\"大虾1\\", \\"price\\":12.3, " +

164 "\\"imagePath\\":\\"http://192.168.10.165:8080/f1.jpg\\"}," +

165 "{\\"id\\":4, \\"name\\":\\"大虾2\\", \\"price\\":12.5, " +

166 "\\"imagePath\\":\\"http://192.168.10.165:8080/f2.jpg\\"}]";167

168 List list=new Gson().fromJson(jsonstr, new TypeToken>() {169 }.getType());170

171 et_3.setText(list.toString());172

173

174 }175

176

177 //Gson对象转JSON

178 public voidbt5_OnClick(View v)179 {180 //对象转JSON

181 ShopInfo info=new ShopInfo(3,"http://www.sina.com","KK",1000);182

183 String json=newGson().toJson(info);184

185 et_3.setText(json);186

187

188 }189

190 //Gson集合转JSON

191 public voidbt6_OnClick(View v)192 {193 //集合转JSON

194 List list=new ArrayList();195

196 list.add(new ShopInfo(3, "http://www.sina.com","KK", 1000));197 list.add(new ShopInfo(4, "http://www.sina.com/cn", "KK1", 2000));198 String json=newGson().toJson(list);199

200 et_3.setText(json);201

202

203 }204

205

206 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值