当前位置:首页 » 安卓系统 » androidjson遍历

androidjson遍历

发布时间: 2023-03-18 22:50:33

⑴ android 解析json二维数组

javascript的语法存取和解析。你例子中有明显错误,js的数组和对象不分,php也不可能生成这样的json。
按javascript的语法存取和解析。学会js,按js的规矩办。

php下可用$a=json_decode()解码这个串,然后按js的规矩
echo $a[0]['uname'];显示York
echo $a[0]['tag']['2'];显示北京
可以用foreach逐层遍历,.和PHP的数组同样的。

⑵ android怎么遍历jsonobject

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);
• }
• }

⑶ 解析json的数据

一则册、 JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧。
Json建构于两种结构:
1、“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对孙厅宏象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。 如:
{
“name”:”jackson”,
“age”:100
}

2、值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)如:
{
“students”:
[
{“伏枝name”:”jackson”,“age”:100},
{“name”:”michael”,”age”:51}
]
}
二、java解析JSON步骤
A、服务器端将数据转换成json字符串
首先、服务器端项目要导入json的jar包和json所依赖的jar包至builtPath路径下(这些可以到JSON-lib官网下载:)

然后将数据转为json字符串,核心函数是:
public static String createJsonString(String key, Object value)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, value);
return jsonObject.toString();
}
B、客户端将json字符串转换为相应的javaBean
1、客户端获取json字符串(因为android项目中已经集成了json的jar包所以这里无需导入)
public class HttpUtil
{

public static String getJsonContent(String urlStr)
{
try
{// 获取HttpURLConnection连接对象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
// 设置连接属性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 获取相应码
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}

private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相当于内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流转移到内存输出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 将内存流转换为字符串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
2、获取javaBean
public static Person getPerson(String jsonStr)
{
Person person = new Person();
try
{// 将json字符串转换为json对象
JSONObject jsonObj = new JSONObject(jsonStr);
// 得到指定json key对象的value对象
JSONObject personObj = jsonObj.getJSONObject("person");
// 获取之对象的所有属性
person.setId(personObj.getInt("id"));
person.setName(personObj.getString("name"));
person.setAddress(personObj.getString("address"));
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return person;
}

public static List<Person> getPersons(String jsonStr)
{
List<Person> list = new ArrayList<Person>();

JSONObject jsonObj;
try
{// 将json字符串转换为json对象
jsonObj = new JSONObject(jsonStr);
// 得到指定json key对象的value对象
JSONArray personList = jsonObj.getJSONArray("persons");
// 遍历jsonArray
for (int i = 0; i < personList.length(); i++)
{
// 获取每一个json对象
JSONObject jsonItem = personList.getJSONObject(i);
// 获取每一个json对象的值
Person person = new Person();
person.setId(jsonItem.getInt("id"));
person.setName(jsonItem.getString("name"));
person.setAddress(jsonItem.getString("address"));
list.add(person);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

⑷ json数据请问怎么遍历

如果是js中遍历使用
var anObject = {one:1,two:2,three:3};//对json数组each
$.each(anObject,function(name,value) {

});

如果是Java代码直接用for循环就行了,说白了json也是数组的一种,json对象和json数组都可以

//遍历json数组
String json1 = "{data:[{name:'Wallace'},{name:'Grommit'}]}";
jsonObjSplit = new JSONObject(json1);
JSONArray ja = jsonObjSplit.getJSONArray("data");
for (int i = 0; i < ja.length(); i++) {JSONObject jo = (JSONObject) ja.get(i);System.out.println(jo.get("name"));}
//JSONObject遍历json对象
String json2 = "{name:'Wallace',age:15}";
jsonObj = new JSONObject(json2);
for (Iterator iter = jsonObj.keys(); iter.hasNext();) {String key = (String)iter.next();System.out.println(jsonObj .getString(Key));}

⑸ android中将json转为list

Stringjson="{'斗脊count':1,'gid':100000,'id':1,'text':'100000'},{'count':70484,'gid':100000,'id':1,'text':'101252'}";
//把json字符串转化为json对象
JSONObjectjsonObject=JSONObject.fromObject(json);
ArrayList<obj>list=newArrayList<obj>();
//遍历json对象
for(inti=0;i<jsonObject.size();i++){
Strings=jsonObject.getString(i);
JSONObjectdata=JSONObject.fromObject(s);
objobj1=newobj();
obj1.list_id=data.getString("id");
空贺渗obj1.list_text=data.getString("text"拍闭);
list.add(obj1);
}

classobj{
privatestringlist_id;
privatestringlist_text;
}

⑹ android中JSONArray和ArrayList有什么区别可以相互转化吗

JsonArray 遍历 获得 JsonObject,JsonObject 转 java Object,再添加到ArrayList

⑺ 怎么将json字符串变成json对象并遍历

1>jQuery插件支持的转换方式:

复制代码代码如下:

$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象

2>浏览器支持的转换方式(Firefox,chrome,opera,safari,ie9,ie8)等浏览器:

复制代码代码如下:

JSON.parse(jsonstr); //亩扮可以将json字符串转换成json对象
JSON.stringify(jsonobj); //可以将json对象转换成json对符串

注:ie8(兼容模式),ie7和ie6没有JSON对象,推荐采用JSON官方的方式,引入json.js。

3>Javascript支持的转换方式:
eval('(' + jsonstr + ')'); //可以将json字符串转换成json对象,注意需要在json字符外包裹一对小括号
注:ie8(兼容模式),ie7和ie6也可以使用eval()将字符串转为JSON对象,但不推荐这些方式,这种方式不安全eval会执行json串中的表达式迅衫灶。

4>JSON官方的转换方式:
http://www.json.org/提供了一个json.js,这样ie8(兼容模式),ie7和ie6就可以支持JSON对象以及其stringify()和parse()方法;
可以塌州在https://github.com/douglascrockford/JSON-js上获取到这个js,一般现在用json2.js。

⑻ Android Json串中添加转义符

一:解析普通json

1:不带转化字符

格式{"type":"ONLINE_SHIPS","message":{"currentTime":1400077615368,"direction":0,"id":1,"latitude":29.5506,"longitude":106.6466}}

JSONObject jsonObject = new JSONObject(jsonstr).getJSONObject("message");
System.out.println("currentTime:"+jsonObject.get("currentTime"));
System.out.println("direction:"+jsonObject.get("direction"));
System.out.println("latitude:"+jsonObject.get("latitude"));
System.out.println("longitude:"+jsonObject.get("longitude"));

jsonarray
JSONObject jo = ja.getJSONArray("cargoList").getJSONObject(0);

2:带转义字符的json格式

{"type":"ONLINE_SHIPS","message":"{\"currentTime\":1400077615368,\"direction\":0,\"id\":1,\"latitude\":29.5506,\"longitude\":106.6466}"}

其实也很简单,先把它转化成字符串就可以了

JSONObject jsonObject = new JSONObject(jsonstr);
//先通过字符串的方式得到,转义字符自然会被转化掉
String jsonstrtemp = jsonObject.getString("message");
System.out.println("message:"+jsonstrtemp);
jsonObject = new JSONObject(jsonstrtemp);
System.out.println("currentTime:"+jsonObject.get("currentTime"));
System.out.println("direction:"+jsonObject.get("direction"));
System.out.println("latitude:"+jsonObject.get("latitude"));
System.out.println("longitude:"+jsonObject.get("longitude"));

二:遍历Json对象

JSONObject ports = ja.getJSONObject("ports");
Iterator<String> keys = ports.keys();
while(keys.hasNext()){
String key=keys.next();
String value = ports.getString(key);
}

三:使用Gjson,json与对象相互转化

使用Gson轻松将java对象转化为json格式

String json = gson.toJson(Object);//得到json形式的字符串

User user = gson.fromJson(json,User.class);//得到对象

转化成list
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lc.function.Action;
import com.lc.models.Groups;

public class MapSearch {

private void ParseData(String _data)
{
Gson gson = new Gson();
List<Groups> ps = gson.fromJson(_data, new TypeToken<List<Groups>>(){}.getType());
System.out.println(ps.get(0).getGroup_name());
}
}

⑼ axio如何遍历json数组

axios.get('./static/test.json').then(res => {
//橡雀 使用ajax请求数据获取到users(数组),所以this.users是数组
this.users= res.data.user
})
}

如果你想获取每芦差个user的start可以使用for循环,当然在vue模板渲染里使用的是v-for例如:
<li v-for="user in users">satrt:<span>{{user.start}}</span></li>

如果只是想在js里面单独获取某个user的start可以直接梁哗早在数组中取一下,例如

// 获取第一个用户的start
var start1 = this.users[0].start

// 获取第二个用户的address
var address2 = this.users[1].address

热点内容
第六章编译原理答案 发布:2025-07-04 17:37:55 浏览:38
php内存优化 发布:2025-07-04 17:25:54 浏览:662
威纶触摸屏如何设置时间限制密码 发布:2025-07-04 17:25:50 浏览:417
python列表的遍历 发布:2025-07-04 17:24:20 浏览:22
编译基本块 发布:2025-07-04 17:23:06 浏览:748
scl语言编程 发布:2025-07-04 17:23:05 浏览:991
oracle用户连接数据库连接 发布:2025-07-04 17:20:20 浏览:938
我的世界纯生存服务器推荐死亡不掉落 发布:2025-07-04 17:06:14 浏览:346
方舟编译器可以用于p20吗 发布:2025-07-04 17:00:17 浏览:785
短片解压 发布:2025-07-04 16:50:08 浏览:737