最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android原生JSON解析實(shí)例

 更新時(shí)間:2017年11月23日 11:08:07   投稿:laozhang  
通過(guò)實(shí)例給大家詳細(xì)分析一下關(guān)于android開發(fā)原生JSON解析的問(wèn)題。

我們?cè)赼ndroid項(xiàng)目開發(fā)的時(shí)候,經(jīng)常要對(duì)JSON進(jìn)行解析,很多朋友在尋找相關(guān)的實(shí)例,小編整理詳細(xì)的相關(guān)分析說(shuō)明,一起來(lái)看下。

JSONObject:JSON數(shù)據(jù)封裝對(duì)象

JSONArray:JSON數(shù)據(jù)封裝數(shù)組

<?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數(shù)據(jù)"/>

 <Button
  android:id="@+id/parse_btn"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="解析json數(shù)據(jù)"/>

 <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文件存在外部存儲(chǔ)中,使用IO流讀取文件中的數(shù)據(jù)
 * 2、使用JSONObject解析讀取出來(lái)的數(shù)據(jù)
 */
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數(shù)據(jù)
 // 遇到{}就創(chuàng)建JSONObject,遇到[]就創(chuàng)建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數(shù)據(jù)
 private void readFile() {
  // 根目錄
  // Environment.getExternalStorageDirectory()

  // 外部存儲(chǔ)公共路徑,例如:DCIM,DOWNLOADS等系統(tǒng)提供的文件夾
//  Environment.getExternalStoragePublicDirectory(類型)

  // 外部存儲(chǔ)私有路徑: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);
 }
}

權(quán)限:

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

如果上面說(shuō)的還有不明白的,大家可以在下方留言討論。

相關(guān)文章

  • Android RecyclerView打造自動(dòng)循環(huán)效果

    Android RecyclerView打造自動(dòng)循環(huán)效果

    這篇文章主要為大家詳細(xì)介紹了android RecyclerView打造自動(dòng)循環(huán)效果,非常實(shí)用的循環(huán)滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android無(wú)需讀寫權(quán)限通過(guò)臨時(shí)授權(quán)讀寫用戶文件詳解

    Android無(wú)需讀寫權(quán)限通過(guò)臨時(shí)授權(quán)讀寫用戶文件詳解

    這篇文章主要為大家介紹了Android無(wú)需讀寫權(quán)限通過(guò)臨時(shí)授權(quán)讀寫用戶文件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android帶圓形數(shù)字進(jìn)度的自定義進(jìn)度條示例

    Android帶圓形數(shù)字進(jìn)度的自定義進(jìn)度條示例

    本篇文章主要介紹了Android帶圓形數(shù)字進(jìn)度的自定義進(jìn)度條示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • Android自定義實(shí)現(xiàn)可回彈的ScollView

    Android自定義實(shí)現(xiàn)可回彈的ScollView

    這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)可回彈的ScollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案

    詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案

    這篇文章主要介紹了詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • Android 利用三階貝塞爾曲線繪制運(yùn)動(dòng)軌跡的示例

    Android 利用三階貝塞爾曲線繪制運(yùn)動(dòng)軌跡的示例

    本篇文章主要介紹了Android 利用三階貝塞爾曲線繪制運(yùn)動(dòng)軌跡的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Android實(shí)現(xiàn)MVVM架構(gòu)數(shù)據(jù)刷新詳解流程

    Android實(shí)現(xiàn)MVVM架構(gòu)數(shù)據(jù)刷新詳解流程

    MVVM架構(gòu)模式,即Model-View-ViewModel三個(gè)層級(jí),MVVM模式出來(lái)的時(shí)間已經(jīng)很長(zhǎng)了,網(wǎng)上關(guān)于MVVM模式的解析也有很多,我這里只說(shuō)一下我自己的理解,基本上是和MVP模式相比較的一個(gè)差異
    2021-10-10
  • 捕獲與解析Android NativeCrash

    捕獲與解析Android NativeCrash

    Android 開發(fā)中,NE一直是不可忽略卻又異常難解的一個(gè)問(wèn)題,原因是這里面涉及到了跨端開發(fā)和分析,需要同時(shí)熟悉 Java,C&C++,并且需要熟悉 NDK開發(fā),并且解決起來(lái)不像 Java異常那么明了,本文為了解決部分疑惑,將從NE的捕獲,解析與還原等三個(gè)方面進(jìn)行探索
    2021-06-06
  • Flutter?隊(duì)列任務(wù)的實(shí)現(xiàn)

    Flutter?隊(duì)列任務(wù)的實(shí)現(xiàn)

    本文主要介紹了Flutter?隊(duì)列任務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • android工程下不能運(yùn)行java main程序的解決方法

    android工程下不能運(yùn)行java main程序的解決方法

    這篇文章主要介紹了android工程下不能運(yùn)行java main程序的解決方法,需要的朋友可以參考下
    2014-05-05

最新評(píng)論

博爱县| 工布江达县| 敖汉旗| 淮北市| 汝阳县| 西乌珠穆沁旗| 新绛县| 宝兴县| 吉安县| 五大连池市| 威信县| 通化县| 都安| 闽清县| 泾源县| 西林县| 安康市| 左云县| 原阳县| 安平县| 怀来县| 衡东县| 淮滨县| 东兴市| 平南县| 洮南市| 涡阳县| 武威市| 永安市| 若羌县| 瑞丽市| 梧州市| 澄城县| 通城县| 黔南| 玉林市| 永宁县| 资兴市| 涿鹿县| 江永县| 灵山县|