json Gson
package com.example.volleylearn;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.test.AndroidTestCase;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/*
1. 将json格式的字符串{}转换为Java对象, 使用原生API
2. 将json格式的字符串{}转换为Java对象, 使用GSON
3. 将json格式的字符串[]转换为Java对象的List, 使用原生API
4. 将json格式的字符串[]转换为Java对象的List, 使用GSON
5. 将Java对象转换为json字符串{}, 使用GSON
6. 将Java对象的List转换为json字符串[], 使用GSON
*/
public class JsonTest extends AndroidTestCase{
/*
* 1. 将json格式的字符串{}转换为Java对象, 使用原生API
*/
public void testJsonToObject() throws JSONException {
String jsonString = "{\"id\":2, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}";
//将json字符串封装为JSONObject对象
JSONObject jsonObject = new JSONObject(jsonString);
//从对象中根据key得到对应的value
int id = jsonObject.getInt("id");
String name = jsonObject.getString("name");
double price = jsonObject.getDouble("price");
String imagePath = jsonObject.getString("imagePath");
//封装ShopInfo对象
ShopInfo shopInfo = new ShopInfo(id, name, price, imagePath);
Log.e("TAG", shopInfo.toString());
}
/*
* 1. 将json格式的字符串{}转换为Java对象, 使用GSON
*/
public void testJsonToObject2() {
String jsonString = "{\"id\":3, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}";
ShopInfo shopInfo = new Gson().fromJson(jsonString, ShopInfo.class);
Log.e("TAG", shopInfo.toString());
}
/*
* 3. 将json格式的字符串[]转换为Java对象的List, 使用原生API
*/
public void testJsonToList() throws JSONException {
String jsonString = "[{\"id\":3, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"},"
+ "{\"id\":5, \"name\":\"大虾2\", \"price\":128.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f2.jpg\"}]";
List<ShopInfo> list = new ArrayList<ShopInfo>();
//1. 将json字符串包装JSONArray对象
JSONArray jsonArray = new JSONArray(jsonString);
//2. 遍历JSONArray对象所有元素(JSONObject), 并将每个元素封装为shopInfo, 并添加到List
for(int i=0;i<jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//从对象中根据key得到对应的value
int id = jsonObject.getInt("id");
String name = jsonObject.getString("name");
double price = jsonObject.getDouble("price");
String imagePath = jsonObject.getString("imagePath");
//封装ShopInfo对象
ShopInfo shopInfo = new ShopInfo(id, name, price, imagePath);
list.add(shopInfo);
}
Log.e("TAG", list.toString());
}
/*
* 4. 将json格式的字符串[]转换为Java对象的List, 使用GSON
*/
public void testJsonToList2() throws JSONException {
String jsonString = "[{\"id\":4, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"},"
+ "{\"id\":6, \"name\":\"大虾2\", \"price\":128.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f2.jpg\"}]";
List<ShopInfo> list = new Gson().fromJson(jsonString, new TypeToken<List<ShopInfo>>(){}.getType());
Log.e("TAG", list.toString());
}
/*
5. 将Java对象转换为json字符串{}, 使用GSON
*/
public void testObjectToJson() {
ShopInfo info = new ShopInfo(3, "KK", 1000, "http://www.sina.com");
String json = new Gson().toJson(info);
Log.e("TAG", json);
}
/*
6. 将Java对象的List转换为json字符串[], 使用GSON
*/
public void testListToJson() {
List<ShopInfo> list = new ArrayList<ShopInfo>();
list.add(new ShopInfo(3, "KK", 1000, "http://www.sina.com"));
list.add(new ShopInfo(4, "KK2", 2000, "http://www.sina.com222"));
String json = new Gson().toJson(list);
Log.e("TAG", json);
}
public void testJsonToMap() {
String jsonString = "{\"my name\":\"大虾\", \"1\":12}";
Map<String, Object> map = new Gson().fromJson(jsonString, new TypeToken<Map<String, Object>>(){}.getType());
Log.e("TAG", map.toString());
}
}
FastJson
// (4)将Java对象的List转换为json字符串[]
private void javaToJsonArrayByFastJson() {
// 1 创建一个Java集合
List<ShopInfo> shops = new ArrayList<>();
ShopInfo baoyu = new ShopInfo(1, "鲍鱼", 250.0, "baoyu");
ShopInfo longxia = new ShopInfo(2, "龙虾", 251.0, "longxia");
shops.add(baoyu);
shops.add(longxia);
// 2 生成JSON数据
String json = JSON.toJSONString(shops);
}
// (3)将Java对象转换为json字符串{}
private void javaToJsonObjectByFastJson() {
// 1 创建一个Java对象
ShopInfo shopInfo = new ShopInfo(1, "鲍鱼", 250.0, "baoyu");
// 2 生成JSON数据
String json = JSON.toJSONString(shopInfo);
}
// (2)将json格式的字符串[]转换为Java对象的List
private void jsonToJavaListByFastJson() {
// 1 获取或创建json数据
String json = "[\n" +
" {\n" +
" \"id\": 1,\n" +
" \"imagePath\": \"http://192.168.10.165:8080/f1.jpg\",\n" +
" \"name\": \"大虾1\",\n" +
" \"price\": 12.3\n" +
" },\n" +
" {\n" +
" \"id\": 2,\n" +
" \"imagePath\": \"http://192.168.10.165:8080/f2.jpg\",\n" +
" \"name\": \"大虾2\",\n" +
" \"price\": 12.5\n" +
" }\n" +
"]";
// 2 解析JSON数据
List<ShopInfo> shopInfos = JSON.parseArray(json, ShopInfo.class);
}
// (1)将json格式的字符串{}转换为Java对象
private void jsonToJavaObjectByFastJson() {
// 1 获取或创建json数据
String json = "{\n" +
"\t\"id\":2, \"name\":\"大虾\", \n" +
"\t\"price\":12.3, \n" +
"\t\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"\n" +
"}\n";
// 2 解析JSON数据
ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class);
}
}
json Gson的更多相关文章
- Json,Gson,FastJson解析笔记
Json,Gson,FastJson解析笔记 1.将JavaBean转换成Json对象: public static String CreatJsonFromObject(Object key,Obj ...
- Android JSON,Gson,fastjson实现比较
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Json,Gson,Ajax基础知识
//json 是一种轻量级的文本格式,解析简单,他也是一键值来存,数据与数据的分割是以,来分割 //{} 看到大括号就是一个对象,[]代表集合 ,基本上所有数据的交互都是以json格式来进行传递的 / ...
- AJAX,JSON,GSON
AJAX将数据使用JSON格式发送给后端Servlet或其他语言解析. 对JSON内容使用GSON外扩展包进行分解,并使用(如查询用户名是否已经被注册), 最后使用Map集合设置新的返回状态码,并使用 ...
- Web jsp开发自学——ajax+servlet+echarts+json+gson 实现ajax传输servlert和echarts的数据,可视化结果
感谢下面的博主,我学习的博客有: https://blog.csdn.net/ITBigGod/article/details/81023802 Jsp+Servlet+Echarts实现动态数据可 ...
- android Json Gson FastJson 解析
一 Json xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- json(gson) 转换html标签带来的麻烦
gson 转换html标题时,会把html(特殊字符转换为unicode编码) ,所以为了避免这个问题GsonBuilder类 有一个 disablehtmlEscaping方法. 就可以让gson类 ...
- java json Gson
引入 Gson 到 pom.xml <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <de ...
- Gson操作json
github:https://github.com/google/gson API:http://google.github.io/gson/apidocs/ 示例对象 package present ...
随机推荐
- hdu 2841(容斥原理+状态压缩)
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- js库-AngularJS
我是一个不思进取的前端. 我想按部就班的工作. 我想得过且过. 老天呀!你咋又逼迫我学习对于我来说的新知识呢!!!!!!!我想哭呀!!!!! 在某个代码项目中我看到了{{??}}这么个标记!我的神呀! ...
- kettle变量使用
公司项目使用kettle重构之前的取数,先研究下日常的使用. 一.建立数据转换,表数据到表输出,其中表输入数据来自其他业务数据库,通过输入sql执行得到数据. 表输入: 表输出: 设置并行4个线程. ...
- Java创建和解析Json数据方法(三)——json-lib包的使用
(三)json-lib包的使用 这篇笔记主要介绍json-lib包的创建和解析json数据的方式,主要是的JSONObject.JSONArray和Java对象:beans, maps ...
- android特效集合
https://github.com/Trinea/android-open-project http://www.cnblogs.com/hawkon/p/3593709.html http://i ...
- Kaggle的Outbrain点击预测比赛分析
https://yq.aliyun.com/articles/293596 https://www.kaggle.com/c/outbrain-click-prediction https://www ...
- Cucumber+Rest Assured快速搭建api自动化测试平台
转载:http://www.jianshu.com/p/6249f9a9e9c4 什么是Cucumber?什么是BDD?这里不细讲,不懂的直接查看官方:https://cucumber.io/ 什么是 ...
- sklearn特征选择和分类模型
sklearn特征选择和分类模型 数据格式: 这里.原始特征的输入文件的格式使用libsvm的格式,即每行是label index1:value1 index2:value2这样的稀疏矩阵的格式. s ...
- Linux包括hash_map和hash_set的not declared问题
当在Linux下cpp文件包括hash_map或hash_set时.会出现"'hash_map' was not declared in this scope"问题. #inclu ...
- Food hub
Work center List Tillage 耕作 Hand harvest 手工采收 Planting 种植 Cultivating 培养 Mulching 覆盖 Dig harvest 挖地采 ...