사용자 도구

사이트 도구


android:json파싱하기
json파싱하기

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
android:json파싱하기 [2025/04/23 23:24] 이거니맨android:json파싱하기 [2025/04/23 23:37] (현재) 이거니맨
줄 306: 줄 306:
  
 <code kotlin> <code kotlin>
 +// 스트링에서 JSON 배열을 돌려주는 함수 
 fun getJsonAnswerText(jsonText : String) : ArrayList<AnswerText> { fun getJsonAnswerText(jsonText : String) : ArrayList<AnswerText> {
  
-    val jsonArray = JSONArray(jsonText)+    try { 
 +        val jsonArray = JSONArray(jsonText)
  
-    val textArray = ArrayList<AnswerText>(jsonArray.length())+        val textArray = ArrayList<AnswerText>(jsonArray.length())
  
-    for (i in 0 until jsonArray.length()) { +        for (i in 0 until jsonArray.length()) { 
-        val jsonObject = jsonArray[i] as JSONObject+            val jsonObject = jsonArray[i] as JSONObject
  
-        val valueOfAnswer : AnswerText = AnswerText( +            val valueOfAnswer : AnswerText = AnswerText( 
-            text = jsonObject.optString("text", "").first(), +                text = jsonObject.optString("text", "").first(), 
-            correctness = jsonObject.optString("correctness", "false").toBoolean(), +                correctness = jsonObject.optString("correctness", "false").toBoolean(), 
-            index = jsonObject.optString("index", "0").toInt()+                index = jsonObject.optString("index", "0").toInt()
  
-        )+            )
  
-        textArray.add(valueOfAnswer)+            textArray.add(valueOfAnswer
 +        } 
 + 
 +        return textArray 
 +    }catch (e: JSONException) { 
 +        // TODO Auto-generated catch block 
 +        e.printStackTrace()
     }     }
 +    
 +    //  아래 코드는 만약에 에러가 날 경우에만 돌려주는 더미 배열이다. 
 +    val textArray = ArrayList<AnswerText>()
  
     return textArray     return textArray
 +
 } }
  
android/json파싱하기.txt · 마지막으로 수정됨: 2025/04/23 23:37 저자 이거니맨