如何使用Java解析JSON 2D数组 [英] How to parse JSON 2D array using Java

查看:113
本文介绍了如何使用Java解析JSON 2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的json数据:

I have json data like this:

{ 
    "err_code": "0", 
    "date":"20130121",
    "time_from":"1242", 
    "range":"5",
    "data":[['12313123','BOOK CODE CYFV3M
NUM CODE 3789850802600'],['089898989','BOOK CODE 1F45MN
NUM CODE 3787510241500']] 
}

并且我已经尝试过该程序:

and I've tried this program:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonKAI {

    public static void main(String[] args) {

        String jStr = "{\"err_code\":\"0\",\"date\":\"20130121\",\"time_from\":\"1242\",\"range\":\"5\",\"data\":[['12313123','BOOK CODE CYFV3M NUM CODE 3789850802600'],['089898989','BOOK CODE 1F45MN NUM CODE 3787510241500']] }";
// Replace this try catch block for all below subsequent examples
        try {
            JSONObject rootObject = new JSONObject(jStr);
            JSONArray rows = rootObject.getJSONArray("data"); // Get all JSONArray data
            int count = rows.length();
            for(int i=0 ; i< count; i++){
                JSONArray jsonArr = rows.getJSONArray(i);
                System.out.println("jsonObject " + i + ": " + jsonArr);
                //for(int j=0 ; j< count; j++){
                //JSONArray jArr = rows.getJSONArray(j);
                //String s = jArr.toString();
                //System.out.println("jsonObject " +s);
            }}
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

输出为:

jsonObject 0: ["12313123","BOOK CODE CYFV3M NUM CODE 3789850802600"]
jsonObject 1: ["089898989","BOOK CODE 1F45MN NUM CODE 3787510241500"]

我想问一下如何获取/解析 ["12313123",图书代码CYFV3M编号3789850802600"]

I wanna ask how to get/parse from ["12313123","BOOK CODE CYFV3M NUM CODE 3789850802600"]

12313123BOOK CODE CYFV3M NUM CODE 3789850802600(不带"["和"")? 请帮我. 谢谢.

to 12313123 and BOOK CODE CYFV3M NUM CODE 3789850802600 (without '[' and '"')? Please help me. Thank you.

推荐答案

您可以尝试以下方法:

        String jStr = "{\"err_code\":\"0\",\"date\":\"20130121\",\"time_from\":\"1242\",\"range\":\"5\",\"data\":[['12313123','BOOK CODE CYFV3M NUM CODE 3789850802600'],['089898989','BOOK CODE 1F45MN NUM CODE 3787510241500']] }";
        try {
          JSONObject rootObject = JSONObject.fromObject(jStr);
          JSONArray rows = rootObject.getJSONArray("data"); // Get all JSONArray data
          int count = rows.size();
          for (int i = 0; i < count; i++) {
            JSONArray jsonArr = rows.getJSONArray(i);
            System.out.println("jsonArray " + i + ": " + jsonArr);
            for (Object o : jsonArr) {          
              System.out.println(o);
            }
          }
        } catch (JSONException e) {
        }

这篇关于如何使用Java解析JSON 2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆