JSONObject: JSON data encapsulation object
JSONArray: JSON data encapsulation array
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:andro xmlns:app="/apk/res-auto" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=""> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Read json data in file"/> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Parse json data"/> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"/> </LinearLayout>
Bean:
package ; import ; public class ShoppingBean { private String msg; private InfoBean info; public String getMsg() { return msg; } public void setMsg(String msg) { = msg; } public InfoBean getInfo() { return info; } public void setInfo(InfoBean 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) { = good; } public boolean isUrl() { return url; } public void setUrl(boolean url) { = url; } @Override public String toString() { return "InfoBean{" + "cat_, 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) { = 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) { = 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) { = 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. Store the json file in external storage and use IO stream to read the data in the file * 2. Use JSONObject to parse the read data */ public class MainActivity extends AppCompatActivity implements { private String mJson = ""; protected Button mReadFileBtn; protected Button mParseBtn; protected TextView mResultTv; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); (.activity_main); initView(); } @Override public void onClick(View view) { if (() == .read_file_btn) { readFile(); } else if (() == .parse_btn) { ShoppingBean shopping = parseJson(); ("1507", "" + shopping); } } // parse JSON data // Create JSONObject when encountering {}, and create JSONArray when encountering [] private ShoppingBean parseJson() { ShoppingBean shopping = null; try { JSONObject rootObject = new JSONObject(mJson); shopping = new ShoppingBean(); String msg = ("msg"); (msg); JSONObject infoObject = ("info"); info = new (); (info); int catId = ("cat_id"); info.setCat_id(catId); boolean url = ("url"); (url); JSONArray goodsArray = ("goods"); List<> goodsList = new ArrayList<>(); (goodsList); for (int i = 0; i < (); i++) { JSONObject goodsObject = (i); goods = new (); long addTime = ("add_time"); goods.setAdd_time(addTime); String colorCode = ("colorcode"); (colorCode); String currencyPrice = ("currency_price"); goods.setCurrency_price(currencyPrice); String description = ("description"); (description); String goodsName = ("goods_name"); goods.setGoods_name(goodsName); String thumb = ("thumb"); (thumb); (goods); } } catch (Exception e) { (); } return shopping; } // Read JSON data in the file private void readFile() { // Root directory // () // Public path for external storage, such as: folders provided by DCIM, DOWNLOADS and other systems// (type) // External storage private path: Android folder// (null) // downloads folder path String filePath = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) .getAbsolutePath(); String fileName = ""; File file = new File(filePath, fileName); // File character input stream // Input the word slow character to flush BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); String line = new String(); while ((line = ()) != null) { mJson += line; } if (mJson != null) { (mJson); } } catch (Exception e) { (); } finally { try { (); } catch (IOException e) { (); } } } private void initView() { mReadFileBtn = (Button) findViewById(.read_file_btn); (); mParseBtn = (Button) findViewById(.parse_btn); (); mResultTv = (TextView) findViewById(.result_tv); } }
Permissions:
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>
If you don’t understand what you said above, you can leave a message below to discuss.