欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android原生JSON解析实例

程序员文章站 2023-11-06 22:11:28
我们在android项目开发的时候,经常要对json进行解析,很多朋友在寻找相关的实例,小编整理详细的相关分析说明,一起来看下。 jsonobject: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"/>

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