androidjson遍歷
⑴ 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