android Gson解析
来源:互联网 发布:ubuntu开机黑屏 编辑:程序博客网 时间:2024/06/07 06:55
解析一个简单的Json数据都写了这么多代码
谷歌推出了一款Json解析神器——> Gson
那 Gson 是用来干嘛的呢,它是用来将Json数据转换成对象,或将对象转换成Json数据。只需要导入相应 jar 包就可以。
Gson下载地址:http://download.csdn.net
这次的Json数据为:
{name:张三 ,age:26,phone:[131,132],score:[{id:语文,fenshu:100},{id:数学,fenshu:60},{id:化学,fenshu:120}]}
步骤:
①将下载好的Gson jar 包导入到 libs目录下
②创建Gson对象
Gson gson = new Gson();
- 1
③创建需要转成的JavaBean
我这里创建一个 Student 类
Student.java
//Student的属性必须和Json中的KEY值一样。//JsonObject转化成类,JsonArray转化成集合public class Student { public String name; public String age; public ArrayList<String> phone = new ArrayList<String>(); public ArrayList<Score> score = new ArrayList<Score>(); public class Score { public String id; public String fenshu; @Override public String toString() { return "id:" + id + " fenshu:" + fenshu; } } @Override public String toString() { return "name:" + name + " age:" + age + " phone:" + phone.get(0)+ "," + phone.get(1) + " score:" + score.toString(); }}
注:
1、需要生成的类的类名由你自己定义,没有关系;
2、类里面的属性名必须跟Json字段里面的Key是一模一样的;
3、内部嵌套的用[]括起来的部分是一个List
④将Json数据转化成Student对象
Student student = gson.fromJson(JSONDATA, Student.class);
- 1
使用Gson是不是比传统方法更简单呢,赶紧试试吧
核心代码:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.gson.MainActivity" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="gson" android:text="Gson解析"/> <TextView android:textSize="20sp" android:id="@+id/tv" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
MainAcitivity.java
public class MainActivity extends Activity { private TextView tv; //json数据 public static final String JSONDATA = "{name:张三 ,age:26,phone:[131,132],score:[{id:语文,fenshu:100},{id:数学,fenshu:60},{id:化学,fenshu:120}]}"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); } public void gson(View view) { //创建一个Gson对象 Gson gson = new Gson(); //将Json数据转化成Student对象 Student student = gson.fromJson(JSONDATA, Student.class); //将解析的Json数据显示在TextView上 tv.setText(student.toString()); }}
Student.java
public class Student { public String name; public String age; public ArrayList<String> phone = new ArrayList<String>(); public ArrayList<Score> score = new ArrayList<Score>(); public class Score { public String id; public String fenshu; @Override public String toString() { return "id:" + id + " fenshu:" + fenshu; } } @Override public String toString() { return "name:" + name + " age:" + age + " phone:" + phone.get(0)+ "," + phone.get(1) + " score:" + score.toString(); }}
0 0
- Android JSON Gson 解析
- Android Gson 解析
- Android Gson解析
- Android gson解析
- android中Gson解析
- Android Gson解析Json
- Android Gson 解析 Json
- android之gson解析
- android Gson解析问题
- Android json解析--GSON
- android Gson解析1
- Android Gson解析
- android Gson解析
- Android Gson 解析泛型报错
- Android 之 Gson 解析
- android中用gson解析json
- android 变长数据GSON解析
- Android解析json数据(Gson)
- ROC曲线以及评估指标F1-Score, recall, precision-整理版
- android style
- haartraining训练生成xml过程
- HDU 1171.Big Event in HDU【01背包】【5月26】
- mina第一篇:基本demo实现
- android Gson解析
- linux 自行安装lanmp
- [POJ 3040] Allowance (贪心)
- Cocos2dx 3.x移植Android及打包
- Xcode 7 缺少 *.dylib库的解决方法
- 最佳A*算法【转】
- 【转载】dubbo学习
- typeof 预算符
- BZOJ3125: CITY