android中怎么解析原生JSON,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)擁有網(wǎng)站維護(hù)技術(shù)和項目管理團(tuán)隊,建立的售前、實施和售后服務(wù)體系,為客戶提供定制化的成都網(wǎng)站制作、做網(wǎng)站、網(wǎng)站維護(hù)、成都二樞機(jī)房解決方案。為客戶網(wǎng)站安全和日常運維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護(hù)服務(wù)覆蓋集團(tuán)企業(yè)、上市公司、外企網(wǎng)站、成都商城網(wǎng)站開發(fā)、政府網(wǎng)站等各類型客戶群體,為全球上千家企業(yè)提供全方位網(wǎng)站維護(hù)、服務(wù)器維護(hù)解決方案。
JSONObject:JSON數(shù)據(jù)封裝對象
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文件存在外部存儲中,使用IO流讀取文件中的數(shù)據(jù) * 2、使用JSONObject解析讀取出來的數(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() // 外部存儲公共路徑,例如:DCIM,DOWNLOADS等系統(tǒng)提供的文件夾 // 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); } }
權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
關(guān)于android中怎么解析原生JSON問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
當(dāng)前標(biāo)題:android中怎么解析原生JSON
瀏覽路徑:http://jinyejixie.com/article2/iieeic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、電子商務(wù)、用戶體驗、網(wǎng)站設(shè)計、建站公司、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)