android原生JSON解析实例

android原生JSON解析实例,第1张

概述我们在android项目开发的时候,经常要对JSON进行解析,很多朋友在寻找相关的实例,小编整理详细的相关分析说明,一起来看下。

我们在androID项目开发的时候,经常要对JsON进行解析,很多朋友在寻找相关的实例,小编整理详细的相关分析说明,一起来看下。

JsONObject:JsON数据封装对象

JsONArray:JsON数据封装数组

<?xml version="1.0" enCoding="utf-8"?><linearLayout androID:orIEntation="vertical" xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="net.bwIE.Jsonobject.MainActivity"> <button  androID:ID="@+ID/read_file_btn"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:text="读取文件中的Json数据"/> <button  androID:ID="@+ID/parse_btn"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:text="解析Json数据"/> <TextVIEw  androID:ID="@+ID/result_tv"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:text="Hello World!"/></linearLayout>

Bean:

package net.bwIE.Jsonobject;import java.util.List;public class ShopPingBean { private String msg; private InfoBean info; public String getMsg() {  return msg; } public voID setMsg(String msg) {  this.msg = msg; } public InfoBean getInfo() {  return info; } public voID setInfo(InfoBean info) {  this.info = info; } @OverrIDe public String toString() {  return "ShopPingBean{" +    "msg='" + msg + '\'' +    ",info=" + info +    '}'; } public static class InfoBean {  private int cat_ID;  private List<GoodsBean> good;  private boolean url;  public int getCat_ID() {   return cat_ID;  }  public voID setCat_ID(int cat_ID) {   this.cat_ID = cat_ID;  }  public List<GoodsBean> getGood() {   return good;  }  public voID setGood(List<GoodsBean> good) {   this.good = good;  }  public boolean isUrl() {   return url;  }  public voID setUrl(boolean url) {   this.url = url;  }  @OverrIDe  public String toString() {   return "InfoBean{" +     "cat_ID=" + cat_ID +     ",good=" + good +     ",url=" + url +     '}';  }  public static class GoodsBean {   private long add_time;   private String colorcode;   private String currency_price;   private String description;   private String goods_ID;   private String goods_name;   private String thumb;   public long getAdd_time() {    return add_time;   }   public voID setAdd_time(long add_time) {    this.add_time = add_time;   }   public String getcolorcode() {    return colorcode;   }   public voID setcolorcode(String colorcode) {    this.colorcode = colorcode;   }   public String getCurrency_price() {    return currency_price;   }   public voID setCurrency_price(String currency_price) {    this.currency_price = currency_price;   }   public String getDescription() {    return description;   }   public voID setDescription(String description) {    this.description = description;   }   public String getGoods_ID() {    return goods_ID;   }   public voID setGoods_ID(String goods_ID) {    this.goods_ID = goods_ID;   }   public String getGoods_name() {    return goods_name;   }   public voID setGoods_name(String goods_name) {    this.goods_name = goods_name;   }   public String getThumb() {    return thumb;   }   public voID setThumb(String thumb) {    this.thumb = thumb;   }   @OverrIDe   public String toString() {    return "GoodsBean{" +      "add_time=" + add_time +      ",colorcode='" + colorcode + '\'' +      ",currency_price='" + currency_price + '\'' +      ",description='" + description + '\'' +      ",goods_ID='" + goods_ID + '\'' +      ",goods_name='" + goods_name + '\'' +      ",thumb='" + thumb + '\'' +      '}';   }  } }}

Activity:

/** * 1、将Json文件存在外部存储中,使用IO流读取文件中的数据 * 2、使用JsONObject解析读取出来的数据 */public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { private String mJson = ""; protected button mReadfileBtn; protected button mParseBtn; protected TextVIEw mResultTv; @OverrIDe protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  super.setContentVIEw(R.layout.activity_main);  initVIEw(); } @OverrIDe public voID onClick(VIEw vIEw) {  if (vIEw.getID() == R.ID.read_file_btn) {   readfile();  } else if (vIEw.getID() == R.ID.parse_btn) {   ShopPingBean shopPing = parseJson();   Log.d("1507","" + shopPing);  } } // 解析JsON数据 // 遇到{}就创建JsONObject,遇到[]就创建JsONArray private ShopPingBean parseJson() {  ShopPingBean shopPing = null;  try {   JsONObject rootObject = new JsONObject(mJson);   shopPing = new ShopPingBean();   String msg = rootObject.getString("msg");   shopPing.setMsg(msg);   JsONObject infoObject = rootObject.getJsONObject("info");   ShopPingBean.InfoBean info = new ShopPingBean.InfoBean();   shopPing.setInfo(info);   int catID = infoObject.getInt("cat_ID");   info.setCat_ID(catID);   boolean url = infoObject.getBoolean("url");   info.setUrl(url);   JsONArray goodsArray = infoObject.getJsONArray("goods");   List<ShopPingBean.InfoBean.GoodsBean> goodsList = new ArrayList<>();   info.setGood(goodsList);   for (int i = 0; i < goodsArray.length(); i++) {    JsONObject goodsObject = goodsArray.getJsONObject(i);    ShopPingBean.InfoBean.GoodsBean goods = new ShopPingBean.InfoBean.GoodsBean();    long addTime = goodsObject.getLong("add_time");    goods.setAdd_time(addTime);    String colorCode = goodsObject.getString("colorcode");    goods.setcolorcode(colorCode);    String currencyPrice = goodsObject.getString("currency_price");    goods.setCurrency_price(currencyPrice);    String description = goodsObject.getString("description");    goods.setDescription(description);    String goodsname = goodsObject.getString("goods_name");    goods.setGoods_name(goodsname);    String thumb = goodsObject.getString("thumb");    goods.setThumb(thumb);    goodsList.add(goods);   }  } catch (Exception e) {   e.printstacktrace();  }  return shopPing; } // 读取文件中的JsON数据 private voID readfile() {  // 根目录  // Environment.getExternalStorageDirectory()  // 外部存储公共路径,例如:DCIM,DOWNLOADS等系统提供的文件夹//  Environment.getExternalStoragePublicDirectory(类型)  // 外部存储私有路径:AndroID文件夹//  context.getExternalfilesDir(null)  // downloads文件夹路径  String filePath = Environment    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)    .getabsolutePath();  String filename = "Json.txt";  file file = new file(filePath,filename);  // 文件字符输入流  // 字缓符输入冲流  BufferedReader br = null;  try {   br = new BufferedReader(new fileReader(file));   String line = new String();   while ((line = br.readline()) != null) {    mJson += line;   }   if (mJson != null) {    mResultTv.setText(mJson);   }  } catch (Exception e) {   e.printstacktrace();  } finally {   try {    br.close();   } catch (IOException e) {    e.printstacktrace();   }  } } private voID initVIEw() {  mReadfileBtn = (button) findVIEwByID(R.ID.read_file_btn);  mReadfileBtn.setonClickListener(MainActivity.this);  mParseBtn = (button) findVIEwByID(R.ID.parse_btn);  mParseBtn.setonClickListener(MainActivity.this);  mResultTv = (TextVIEw) findVIEwByID(R.ID.result_tv); }}

权限:

<uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE"/>

如果上面说的还有不明白的,大家可以在下方留言讨论。

总结

以上是内存溢出为你收集整理的android原生JSON解析实例全部内容,希望文章能够帮你解决android原生JSON解析实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143615.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存