android怎么遍历jsonobject
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android怎么遍历jsonobject相关的知识,希望对你有一定的参考价值。
参考技术A android 读取json数据(遍历JSONObject和JSONArray)•public String getJson()
• String jsonString = "\"FLAG\":\"flag\",\"MESSAGE\":\"SUCCESS\",\"name\":[\"name\":\"jack\",\"name\":\"lucy\"]";//json字符串
• try
• JSONObject result = new JSONObject(jsonstring);//转换为JSONObject
• int num = result.length();
• JSONArray nameList = result.getJSONArray("name");//获取JSONArray
• int length = nameList.length();
• String aa = "";
• for(int i = 0; i < length; i++)//遍历JSONArray
• Log.d("debugTest",Integer.toString(i));
• JSONObject oj = nameList.getJSONObject(i);
• aa = aa + oj.getString("name")+"|";
•
•
• Iterator<?> it = result.keys();
• String aa2 = "";
• String bb2 = null;
• while(it.hasNext())//遍历JSONObject
• bb2 = (String) it.next().toString();
• aa2 = aa2 + result.getString(bb2);
•
•
• return aa;
• catch (JSONException e)
• throw new RuntimeException(e);
•
•
fastjson中JSONObject遍历怎么不是有序的
参考技术A 要使用Fastjson,首先需要下载相对应的jar文件,在官网即可下载。附上初学的第一个例子,多多指教:
复制代码
"statuses":[
"id": 912345678901,
"text": "How do I stream JSON in Java?",
"geo": null,
"user":
"name": "json_newb",
"followers_count": 41
,
"id": 777777777888,
"text": "dfngsdnglnsldfnsl",
"geo": null,
"user":
"name": "dsfgpd",
"followers_count": 24
]
复制代码
AllBean的Bean类:
复制代码
package com.lee.JsonToBean;
public class AllBean
private long id;
private String text;
private String geo;
private UserBean userBean;
public long getId()
return id;
public void setId(long id)
this.id = id;
public String getText()
return text;
public void setText(String text)
this.text = text;
public String getGeo()
return geo;
public void setGeo(String geo)
this.geo = geo;
public UserBean getUserBean()
return userBean;
public void setUserBean(UserBean userBean)
this.userBean = userBean;
复制代码
UserBean的Bean类:
复制代码
package com.lee.JsonToBean;
public class UserBean
private String name;
private int followers_count;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getFollowers_count()
return followers_count;
public void setFollowers_count(int followers_count)
this.followers_count = followers_count;
复制代码
解析类JsonBean:
复制代码
package com.lee.JsonToBean;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
*
"statuses":[
"id": 912345678901,
"text": "How do I stream JSON in Java?",
"geo": null,
"user":
"name": "json_newb",
"followers_count": 41
,
"id": 777777777888,
"text": "dfngsdnglnsldfnsl",
"geo": null,
"user":
"name": "dsfgpd",
"followers_count": 24
]
* */
public class JsonBean
RTFEditorKit rtf;
DefaultStyledDocument dsd;
String text;
public static void main(String[] args)
JsonBean bean = new JsonBean();
// 把字符串转为Json对象,这是因为我的json数据首先是json对象
JSONObject jobj = JSON.parseObject(bean.readRtf(new File("json.rtf")));
// 然后是jsonArray,可以根据我的json数据知道
JSONArray arr = jobj.getJSONArray("statuses");
// 根据Bean类的到每一个json数组的项
List<AllBean> listBeans = JSON.parseArray(arr.toString(), AllBean.class);
// 遍历
for(AllBean bean_ : listBeans)
// 我这个demo的json数据获得第一层的数据
System.out.println(bean_.getText());
System.out.println(bean_.getId());
// 我这个demo的json数据获得第二层的数据
System.out.println(bean_.getUserBean().getFollowers_count());
// 因为我把json数据放进rtf文件,这是读取rtf文件的json数据,转化为字符串
public String readRtf(File in)
rtf=new RTFEditorKit();
dsd=new DefaultStyledDocument();
try
rtf.read(new FileInputStream(in), dsd, 0);
text = new String(dsd.getText(0, dsd.getLength()));
catch (FileNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (BadLocationException e)
// TODO Auto-generated catch block
e.printStackTrace();
return text;
本回答被提问者采纳
以上是关于android怎么遍历jsonobject的主要内容,如果未能解决你的问题,请参考以下文章
Android-Android下怎么遍历assets录下的子文件夹
Android-Android下怎么遍历assets录下的子文件夹