簡體   English   中英

在Android中解析JSON字符串

[英]Parse a JSON-string in Android

現在,我有以下JSON字符串:

“{” 1 “:{” 從 “:” 540" , “到”: “1020”}, “2”:{ “從”: “540”, “到”: “1020”}, “3”: { “從”: “540”, “到”: “1020”} “4”:{ “從”: “540”, “到”: “1020”}, “5”:{ “從”:” 540" , “到”: “1020”}, “6”:{ “從”: “540”, “到”: “1020”}, “7”:{ “從”: “540”, “為” : “1020”}}”

我想在Android Studio中對其進行解析,並僅出於這種結果進行迭代:

String day = monday;
int hourstart = from;
int hoursclose = to;

當然,從和到意味着數字。 有人知道JSON解析器的結構如何嗎?

嘗試這個:

           //here jsonString is your json in string format

           JSONObject obj3=new JSONObject(jsonString);
           JSONObject obj4=null;
           //Getting all the keys inside json object with keys- from and to
              Iterator<String> keys= obj3.keys();
              while (keys.hasNext()) 
             {
                   String keyValue = (String)keys.next();
                   obj4 = obj3.getJSONObject(keyValue);
                   //getting string values with keys- from and to
                   String from = obj4.getString("from");
                   String to = obj4.getString("to");
                   int hourstart = Integer.parseInt(from);
                   int hoursclose = Integer.parseInt(to);
                   System.out.println("From : "+ hourstart +" To : "+ hoursclose);
             }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM