gson解析带中括号
来源:互联网 发布:淘宝客最低佣金比例 编辑:程序博客网 时间:2024/05/22 04:48
package animtest.com.example.e531.json;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
InputStream stream = getAssets().open("array.json");
String jsonStr=StreamToString.streamToStr(stream,"utf-8");
//使用gson解析
//method1(jsonStr);
//使用原生解析
//method2(jsonStr);
//使用混合解析
method3(jsonStr);
} catch (Exception e) {
e.printStackTrace();
}
}
private void method3(String jsonStr) throws JSONException {
//集合对象
List<Result> resultList=new ArrayList<>();
//json串最外层是[] ,首先要构造的对象 是 JSONArray
JSONArray array=new JSONArray(jsonStr);
for(int i=0;i<array.length();i++){
JSONObject obj = array.optJSONObject(i);
//使用gson解析
Gson gson=new Gson();
Result r=gson.fromJson(obj.toString(),Result.class);
resultList.add(r);
}
for(int i=0;i<resultList.size();i++){
Log.d("zzz",resultList.get(i).toString());
}
}
private void method2(String jsonStr) throws JSONException {
//集合对象
List<Result> resultList=new ArrayList<>();
//json串最外层是[] ,首先要构造的对象 是 JSONArray
JSONArray array=new JSONArray(jsonStr);
for(int i=0;i<array.length();i++){
JSONObject obj = array.optJSONObject(i);
String name=obj.optString("name");
Result result=new Result();
result.setName(name);
resultList.add(result);
}
for(int i=0;i<resultList.size();i++){
Log.d("zzz",resultList.get(i).toString());
}
}
private void method1(String jsonStr) {
//集合对象
List<Result> resultList=new ArrayList<>();
//得到type类型
Type type=new TypeToken<List<Result>>(){}.getType();
Gson gson=new Gson();
resultList=gson.fromJson(jsonStr,type);
for(int i=0;i<resultList.size();i++){
Log.d("zzz",resultList.get(i).toString());
}
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
InputStream stream = getAssets().open("array.json");
String jsonStr=StreamToString.streamToStr(stream,"utf-8");
//使用gson解析
//method1(jsonStr);
//使用原生解析
//method2(jsonStr);
//使用混合解析
method3(jsonStr);
} catch (Exception e) {
e.printStackTrace();
}
}
private void method3(String jsonStr) throws JSONException {
//集合对象
List<Result> resultList=new ArrayList<>();
//json串最外层是[] ,首先要构造的对象 是 JSONArray
JSONArray array=new JSONArray(jsonStr);
for(int i=0;i<array.length();i++){
JSONObject obj = array.optJSONObject(i);
//使用gson解析
Gson gson=new Gson();
Result r=gson.fromJson(obj.toString(),Result.class);
resultList.add(r);
}
for(int i=0;i<resultList.size();i++){
Log.d("zzz",resultList.get(i).toString());
}
}
private void method2(String jsonStr) throws JSONException {
//集合对象
List<Result> resultList=new ArrayList<>();
//json串最外层是[] ,首先要构造的对象 是 JSONArray
JSONArray array=new JSONArray(jsonStr);
for(int i=0;i<array.length();i++){
JSONObject obj = array.optJSONObject(i);
String name=obj.optString("name");
Result result=new Result();
result.setName(name);
resultList.add(result);
}
for(int i=0;i<resultList.size();i++){
Log.d("zzz",resultList.get(i).toString());
}
}
private void method1(String jsonStr) {
//集合对象
List<Result> resultList=new ArrayList<>();
//得到type类型
Type type=new TypeToken<List<Result>>(){}.getType();
Gson gson=new Gson();
resultList=gson.fromJson(jsonStr,type);
for(int i=0;i<resultList.size();i++){
Log.d("zzz",resultList.get(i).toString());
}
}
}
阅读全文
0 0
- gson解析带中括号
- Gson解析带中括号
- android中Gson解析
- google自带的gson解析
- Android中JSON数据解析:系统自带JSONObject以及第三方GSON的实现
- Android中Gson解析json
- Gson全解析(中)
- 解析中括号json文件
- 中序表达式转后序表达式(带括号)
- Gson 生成和解析带Date类型的json
- Gson 生成和解析带Date类型的json
- Android自带json和Gson数据格式解析示列
- Gson 生成和解析带Date类型的json
- JS函数中带与不带括号的区别
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android 中 使用 Gson解析json
- 蓝桥杯 算法训练 P1103 (复数的计算)
- redhat(rhe)yum-centos源
- 高数 02[01-05]01习题课
- 另类的Android studio 3.0 报错解决方案
- (学习java)用十字链表表示有向图
- gson解析带中括号
- 查看RocketMQ的broker启动部分源码分析总结
- 学习laravel的教程
- 使用Construct 2 制作HTML5游戏
- windows环境下使用wiki中文百科及gensim工具库训练词向量
- HDU5572(2015ACM/ICPC亚洲区上海站A题)_计算几何(积累计算几何的板子)
- struts2学习_第六天(小知识总结)
- Integer to Roman--LeetCode
- mymoni3——实现效果: