2013-02-06 60 views
5

Tôi gặp khó khăn tìm hiểu làm thế nào để tôi lặp qua các đối tượng json dướiVòng qua một đối tượng JSON trong Java

[ 
    {"course_slug":"course-4504","course_description":"A principle refers to a fundamental truth. It establishes cause and effect relationship between t...","course_name":"Principles of Management","course_thumb":"http:\/\/i1117.photobucket.com\/albums\/k594\/thetutlage\/principles_of-management.jpg?t=1359623785"}, 
    {"course_slug":"course-4502","course_description":"Management accounting or managerial accounting is concerned with the provisions and use of accoun...","course_name":"Management Accounting","course_thumb":"http:\/\/i1117.photobucket.com\/albums\/k594\/thetutlage\/management_Accounting.jpg?t=1359623495"}, 
    {"course_slug":"course-4503","course_description":"Quantitative Techniques","course_name":"Quantitative Techniques","course_thumb":""} 
] 

Trả lời

14

bạn có thể lặp qua chuỗi json hiện tại như:

JSONArray jsonarr = new JSONArray("your json String"); 


    for(int i = 0; i < jsonarr.length(); i++){ 

    JSONObject jsonobj = jsonarr.getJSONObject(i); 

    // get course_slug 
    String str_course_slug=jsonobj.getString("course_slug"); 
    // get course_description 
    String str_course_description=jsonobj.getString("course_description"); 
    //... for other elements 
    } 
+0

Dường như đang hoạt động –

+0

@AmanVirk: bạn sẽ cần ArrayList hoặc HaspMap để lưu trữ kết quả phân tích cú pháp và sử dụng nó trong toàn bộ ứng dụng –

1
String data = "[ 
{ 
    "course_slug": "course-4504", 
    "course_description": "A principle refers to a fundamental truth. It establishes cause and effect relationship between t...", 
    "course_name": "Principles of Management", 
    "course_thumb": "http://i1117.photobucket.com/albums/k594/thetutlage/principles_of-management.jpg?t=1359623785" 
}, 
{ 
    "course_slug": "course-4502", 
    "course_description": "Management accounting or managerial accounting is concerned with the provisions and use of accoun...", 
    "course_name": "Management Accounting", 
    "course_thumb": "http://i1117.photobucket.com/albums/k594/thetutlage/management_Accounting.jpg?t=1359623495" 
}, 
{ 
    "course_slug": "course-4503", 
    "course_description": "Quantitative Techniques", 
    "course_name": "Quantitative Techniques", 
    "course_thumb": "" 
} 
]"; 

JSONArray jArray = new JSONArray(data); 

int n = jArray.length; 

for(int i = 0; i < n; i++){ 

JSONObject jObj = jArray.getJSONObject(i); 
} 
1

Hãy thử điều này,

String data = "[ 
     {"course_slug":"course-4504","course_description":"A principle refers to a fundamental truth. It establishes cause and effect relationship between t...","course_name":"Principles of Management","course_thumb":"http:\/\/i1117.photobucket.com\/albums\/k594\/thetutlage\/principles_of-management.jpg?t=1359623785"}, 
     {"course_slug":"course-4502","course_description":"Management accounting or managerial accounting is concerned with the provisions and use of accoun...","course_name":"Management Accounting","course_thumb":"http:\/\/i1117.photobucket.com\/albums\/k594\/thetutlage\/management_Accounting.jpg?t=1359623495"}, 
     {"course_slug":"course-4503","course_description":"Quantitative Techniques","course_name":"Quantitative Techniques","course_thumb":""} 
    ]"; 

JSONArray jArray = new JSONArray(data); 
    for(int i = 0; i < jArray.length(); i++){ 
String str = jarray.getJSONObject(i).getString("course_slug"); 


    } 
1

Dấu ngoặc vuông esents JsonArray. The Curly đại diện cho JsonObjects. Vì vậy, trong ví dụ của bạn, bạn có một JsonArray với 3 JsonObjects.

Hãy "jsonArrayCourse" là JsonArray của bạn, sau đó

 for (int i = 0; i < jsonArrayCourse.length(); i++) { 
      JSONObject c = jsonArrayCourse.getJSONObject(i); 
          String course_slugText = c.getString("course_slug"); 
          String course_descriptionText = 
               c.getString("course_description"); 
          String course_nameText = c.getString("course_name"); 
          String course_thumbURL = c.getString("course_thumb"); 
                  } 

Cũng không quên cố gắng nắm bắt những course_thumpURL trong trường hợp nó không có giá trị gì.

Các vấn đề liên quan