關於兩者區別:可以直接參考這幾位寫的
轉發自:https://blog.csdn.net/Xxacker/article/details/84949591
JSON(一):JSONObject和JSONArray區別及基本用法
一、JSONObject和JSONArray的數據表示形式
JSONObject的數據是用 { } 來表示的
JSONObject jsonObject =
{ "id" : "123", "courseID" : "huangt-test", "title" : "提交作業", "content" : null }
JSONArray是由JSONObject構成的數組,用 [ { } , { } , … , { } ] 來表示
JSONArray jsonArray3 = [
{ "id" : "123", "courseID" : "huangt-test", "title" : "提交作業" },
{ "content" : null, "beginTime" : 1398873600000 "endTime" }
] ;// 表示了包含2個JSONObject的JSONArray。
一個很明顯的區別,一個用的是 { } ,一個最外面用的是 [ ] ;
二、如何將字符串String轉換為JSONObject對象和JSONArray對象?
JSONObject jsonObject = new JSONObject ( String str);
JSONArray jsonArray = new JSONArray(String str ) ;
三、如何從JSONArray中獲得JSONObject對象?
大家可以把JSONArray當成一般的數組來對待,只是獲取的數據內數據的方法不一樣
JSONObject jsonObject = (JSONObject)jsonArray.get(i);
JSONObject jsonObject = jsonArray.getJSONObject(i) ;
兩者都可。第一種注意轉換
四、獲取JSON內的數據
int jid= jsonObject.getInt ( "id" ) ; // 這里的jid得到的數據就是123.
String jcourse=jsonObject.getString( " courseID") ; // 這里的jcourse得到的數據就是huangt-test.
Strirng jcourse = jsonObject.get("courseID").toString();
2. https://www.cnblogs.com/xuanbo/p/6913585.html
JSONObject和JSONArray區別及基本用法
一、JSONObject和JSONArray的數據表示形式
JSONObject的數據是用 { } 來表示的,
例如: { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業", "content" : null }
而JSONArray,顧名思義是由JSONObject構成的數組,用 [ { } , { } , ...... , { } ] 來表示
例如: [ { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業" } , { "content" : null, "beginTime" : 1398873600000 "endTime" } ] ;
表示了包含2個JSONObject的JSONArray。
可以看到一個很明顯的區別,一個用的是 { } ,一個最外面用的是 [ ] ;
二、如何從字符串String獲得JSONObject對象和JSONArray對象
JSONObject jsonObject = new JSONObject ( String str);
JSONArray jsonArray = new JSONArray(String str ) ;
三、如何從JSONArray中獲得JSONObject對象
大家可以把JSONArray當成一般的數組來對待,只是獲取的數據內數據的方法不一樣
JSONObject jsonObject = (JSONObject)jsonArray.get(i);
JSONObject jsonObject = jsonArray.getJSONObject(i) ;
兩者都可。第一種注意轉換
四、獲取JSON內的數據
int jid= jsonObject.getInt ( "id" ) ; // 這里的jid得到的數據就是123.
String jcourse=jsonObject.getString( " courseID") ; // 這里的jcourse得到的數據就是huangt-test.
Strirng jcourse = jsonObject.get("courseID").toString();
五、一般地 為鍵值對
eg:{ name:"xm", value:"張三"}
String jname = null;
if(jsonObject.get("name").equals("xm")){
jname=jsonObject.get("value").toString();
}
六: JSON.parse( str ) --> 把字符串轉為JSON對象
JSON.stringify( obj ) -->把對象解析為字符串
3. 遍歷其中的list或者map: https://blog.csdn.net/soicant/article/details/79318181
var list1 = ["number","name"];
var list2 = ["36","Crown","15","Faker","Swift","68","Dandy"];
var map_demo = { name: "John", lang: "JS" };
1.最常用的for循環
for(var i=0;i<list2.length;i++){
console.info(i +":"+ list2 [i]);
}
改進:這里可以將list2.length提出來,不用每次計算長度,效率更高一些,such as:
var len=list2.length;
for(var i=0;i<len;i++){
console.info(i +":"+ list2 [i]);
}
小結:很常見也很常用,效率也不差,但不能遍歷map。
2.for...in...遍歷List/map
//遍歷map
for(var key in map_demo){
console.info(key+":"+map_demo[key]);
}
//遍歷List
for(var index in list2){
console.info(index+":"+list2[index]);
}
小結:對於List來說,能不用for...in就不要用,效率低下。
3.forEach遍歷List
list2.forEach(function (element, index, array) {
console.info(element); //當前元素的值
console.info(index); //當前下標
console.info(array); //數組本身
});
小結:和for循環效率差不多。
4.$.each()遍歷List/map
//遍歷List
$.each(list2,function(index,items){
console.info(index+":"+items);
});
//遍歷map
$.each(map_demo,function(key,value){
console.info("key: " + key + ", Value: " + value );
})
5.$.map()遍歷List/map
//遍歷List
var new_list = $.map(list2,function(items,index){
return items+"!";
})
console.info(new_list);
//遍歷map
$.map(map_demo,function(key,value){
console.log(key+":"+value);
});
小結:$.map()寫法和$.each()類似,但對list的遍歷時,參數順序和$.each()是相反的,並且可以帶返回值。對map的遍歷和$.each()一樣