【问题标题】:How to parse JSON 2D array using Java如何使用 Java 解析 JSON 2D 数组
【发布时间】:2013-01-27 02:33:59
【问题描述】:

我有这样的json数据:

{ 
    "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']] 
}

我已经尝试过这个程序:

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 NUM 代码 3789850802600"]

12313123BOOK CODE CYFV3M NUM CODE 3789850802600(没有'[' 和'"')? 请帮我。 谢谢。

【问题讨论】:

  • 附带说明,我建议改用 Gson 库。您的二维 json 数组将创建为列表列表。如果您想探索这种可能性,这个答案可能会有所帮助:stackoverflow.com/a/7925602/877472
  • 你有一个数组。提取数组的元素。

标签: java arrays json parsing


【解决方案1】:

你可以试试这个:

        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) {
        }

【讨论】:

    【解决方案2】:

    我有下面的例子:

    String str = "{\"items\":[[1,2,3],[4,5,6],[7,8,9]],\"ans\":[[\"1\",\"2\"],[\"Glad\",\"Good\"]]}";
                try{
                    JSONObject jsonObj = new JSONObject(str);
                    JSONArray jsonArry1 = jsonObj.getJSONArray("items");
    
                    int[][] ints1 = new int[jsonArry1.length()][3];
    
                    for(int i = 0; i<jsonArry1.length(); i++){
                        JSONArray jsa1 = jsonArry1.getJSONArray(i);
    
                        for(int j = 0; j<jsa1.length();j++){
                            ints1[i][j] = jsa1.getInt(j);
                        }
    
                    }
    

    我测试过,它有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 2021-01-17
      • 2012-11-08
      • 2012-01-12
      • 1970-01-01
      相关资源
      最近更新 更多