【移动应用开发技术】android中怎么解析原生JSON_第1页
【移动应用开发技术】android中怎么解析原生JSON_第2页
【移动应用开发技术】android中怎么解析原生JSON_第3页
【移动应用开发技术】android中怎么解析原生JSON_第4页
【移动应用开发技术】android中怎么解析原生JSON_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】android中怎么解析原生JSON

android中怎么解析原生JSON,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。JSONObject:JSON数据封装对象JSONArray:JSON数据封装数组<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

android:orientation="vertical"

xmlns:android="/apk/res/android"

xmlns:app="/apk/res-auto"

xmlns:tools="/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)

{

=

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

BufferedR

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论