json解析的方法

gson 解析:

// Type 是获取当前实体类的类型方法

//json 是网络请求下来的json字符串

//需要Gson.jar包

public static ArrayList<Me_GroupBean> resolvExListData(String json)

throws JSONException {
Type listType = new TypeToken<ArrayList<Me_GroupBean>>() {
}.getType();
Gson gson = new Gson();
ArrayList<Me_GroupBean> list = gson.fromJson(json, listType);
return list;

}

fastJson 解析:

// Json.parseArray是fastJson中的方法

//需要fastJson.jar包

//Data.class 是实体类 

private List<Data>  datas=null;

String json=new String(bytes,"utf-8");

JSONArray array=new JSONObject(json).getJSONArray("data");

//会根据你写的实体类中的数据进行解析

List<BKData> list=JSON.parseArray(array.toString(), Data.class);

//加入到实体类中

datas.add(list);

//原生解析比较笨,也比较实用的方法

public static List<Data> ParserJsonHomePager(String path) throws Exception{
List<Data> list=new ArrayList<Aiqg>();
try {
byte[] byte= readParse(path);
String jsonString=new String(byte,"utf-8");
Log.e("data", byte+"");
Log.e("data", jsonString);
JSONObject object=new JSONObject(jsonString);
JSONArray array=object.getJSONArray("data");
for(int i=0;i<array.length();i++){
Data data=new Data();
JSONObject jsonObject=array.getJSONObject(i);
data.setGoodsname(jsonObject.optString(""));
data.setCurrent_price(jsonObject.optDouble(""));
data.setLike(jsonObject.optInt(""));
data.setBargain_count(jsonObject.optInt(""));
data.setDistance(jsonObject.optDouble(""));
data.setMarket_price(jsonObject.optDouble(""));
JSONArray jsonArray02=jsonObject.getJSONArray("");
data.setImages(jsonArray02.getString(0));
JSONArray jsonArray=jsonObject.getJSONArray("");
for(int j=0;j<jsonArray.length();j++){
JSONObject jsonObject2=jsonArray.getJSONObject(j);
data.setShopname(jsonObject2.getString(""));

}
list.add(data);
Log.e("list", list+" ");
}
return list;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

随手记!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值