{"code":200,"msg":"successful","data":{"1":"unable to fill in the information","3":"the repayment has not been confirmed","4":"other issues"}}
4条回答 默认 最新
关注引自免费微信小程序:皆我百晓生
在安卓开发中,解析后台接口返回的JSON格式数据通常使用Gson、Jackson或org.json库。这里我将展示如何使用org.json库来解析上述返回的数据:
import org.json.JSONObject; // 假设你已经通过网络请求获取到了服务器返回的json字符串responseJson String responseJson = "{\"code\":200,\"msg\":\"successful\",\"data\":{\"1\":\"unable to fill in the information\",\"3\":\"the repayment has not been confirmed\",\"4\":\"other issues\"}}"; // 将json字符串转化为JSONObject对象 JSONObject jsonObject = new JSONObject(responseJson); // 获取code和msg字段的值 int code = jsonObject.getInt("code"); String msg = jsonObject.getString("msg"); // 获取data部分并进一步解析为JSONObject JSONObject dataObject = jsonObject.getJSONObject("data"); // 遍历data中的键值对,并打印出来 Iterator<String> keys = dataObject.keys(); while (keys.hasNext()) { String key = keys.next(); String value = dataObject.getString(key); System.out.println("Key: " + key + ", Value: " + value); } // 或者你可以根据具体的键名获取value String issue1 = dataObject.getString("1"); String issue3 = dataObject.getString("3"); String issue4 = dataObject.getString("4"); System.out.println("Issue 1: " + issue1); System.out.println("Issue 3: " + issue3); System.out.println("Issue 4: " + issue4);这段代码首先将返回的JSON字符串转换为JSONObject对象,然后从该对象中提取出code、msg以及data部分的内容。对于data部分,我们将其转换为另一个JSONObject,并遍历其内部的所有键值对。如果需要单独获取某个键对应的值,直接通过getString方法即可。
本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报 编辑记录解决 1无用