gpt4 book ai didi

java - 在 Android 中解析 JSON 字符串

转载 作者:行者123 更新时间:2023-11-29 20:30:19 24 4
gpt4 key购买 nike

现在我有以下 JSON 字符串:

"{"1":{"from":"540","to":"1020"},"2":{"from":"540","to":"1020"},"3":{"from":"540","to":"1020"},"4":{"from":"540","to":"1020"},"5":{"from":"540","to":"1020"},"6":{"from":"540","to":"1020"},"7":{"from":"540","to":"1020"}}"

我想在 Android Studio 中解析它并迭代所有这些结果:

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

当然 from 和 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);
}

关于java - 在 Android 中解析 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32059399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com