如何将多维JSON数组转换为多维Java数组

时间:2014-11-10 13:04:25

标签: java arrays json multidimensional-array

JSON的响应

[{
    "ETag": "c28c2a1d",
    "Id": "566668257",
    "Links": [{
        "Href": "\/v1\/companies\/566668257",
        "Rel": "self"
    }, {
        "Href": "\/v1\/companies\/566668257",
        "Rel": "parent"
    }],
    "Address": "412 S Van Buren",
    "AddressParsed": {
        "Name": "Van Buren",
        "Number": "412",
        "PreDirectional": "S"
    },
    "City": "Weiner",
    "CompanyName": "Greenway Equipment Inc",
    "FirstName": "John",
    "LastName": "Conner",
    "Location": {
        "Latitude": 35.621407,
        "Longitude": -90.910036
    },
    "Phone": "8706847720",
    "PostalCode": "72479",
    "StateProvince": "AR",
    "ParentCompany": "566668257"
}]

我当前的代码,但我不知道如何获取Links的元素:

public void parseJSON(String parse) throws JSONException {
    System.out.println("parse json");

    JSONArray jsonarray = new JSONArray(parse);

    int parselength = jsonarray.length();

    JSONObject arraynode ;

    String[] ID = new String[parselength];
    String[] Etag = new String[parselength];
    String[] City = new String[parselength];        
    String[] Address = new String[parselength];
    **//String[][] Links = new String[parselength][parselength];** 

    System.out.println("parse"+parselength);

    String strCSV = "";

    strCSV = "ID, Etag, Links, Address\r\n"; 
    for(int i=0; i<parselength-1; i++){

         arraynode = jsonarray.getJSONObject(i);

         ID[i] = arraynode.getString("Id");
         City[i] = arraynode.getString("City");
         Etag[i] = arraynode.getString("ETag");
         Address[i] = arraynode.getString("Address");

         //JSONArray innerLink = new JSONArray(arraynode.getString("Links"));

         strCSV += Etag[i]+","+ID[i]+","+City[i]+","+Address[i]+"\r\n";    //   
    }
}

当我从system.out.println检查它时,我可以在内部循环中看到多维数组数据,但是当我将其转换为CSV时,它不会以CSV格式复制。

可以帮助我解决这个问题吗。


链接是我的JSON数组的一个字段,您可以在JSON数组的响应中看到。它是一个多维数组。

当我执行我的代码时,它会抛出一个链接未找到的错误。

我的问题是如何将此JSON数组转换为Java数组?正如我为其他领域所做的那样。

3 个答案:

答案 0 :(得分:0)

你的for循环应该是直到我&lt; parselength

public void parseJSON(String parse) throws JSONException { System.out.println("parse json");

    JSONArray jsonarray = new JSONArray(parse);

    int parselength = jsonarray.length();

    JSONObject arraynode ;

    String[] ID = new String[parselength];
    String[] Etag = new String[parselength];
    String[] City = new String[parselength];        
    String[] Address = new String[parselength];
    **//String[][] Links = new String[parselength][parselength];** 

    System.out.println("parse"+parselength);

    String strCSV = "";

    strCSV = "ID, Etag, Links, Address\r\n"; 
    for(int i=0; i<parselength; i++){

         arraynode = jsonarray.getJSONObject(i);

         ID[i] = arraynode.getString("Id");
         City[i] = arraynode.getString("City");
         Etag[i] = arraynode.getString("ETag");
         Address[i] = arraynode.getString("Address");

         //JSONArray innerLink = new JSONArray(arraynode.getString("Links"));

         strCSV += Etag[i]+","+ID[i]+","+City[i]+","+Address[i]+"\r\n";    //   
    }
}

答案 1 :(得分:0)

也许这会有所帮助....链接本身就是一个JSONArray,所以你也必须遍历它。

for (int i = 0; i < parselength; i++) {

            arraynode = jsonarray.getJSONObject(i);
            ID[i] = arraynode.getString("Id");
            City[i] = arraynode.getString("City");
            Etag[i] = arraynode.getString("ETag");
            Address[i] = arraynode.getString("Address");

            JSONArray jsonarraylink = new JSONArray(arraynode.getJSONArray("Links").toString());

            int linklength = jsonarraylink.length();
            Links[i] = new String[linklength];
            for (int j = 0; j < linklength; j++) {
                linknode = jsonarraylink.getJSONObject(i);
                Links[i][j] = linknode.getString("Href");
                System.out.println(linknode.getString("Href"));
            }
            //JSONArray innerLink = new JSONArray(arraynode.getString("Links"));

            strCSV += Etag[i] + "," + ID[i] + "," + City[i] + "," + Address[i] + "\r\n";    //   
        }

答案 2 :(得分:0)

public void parseJSON(String parse) throws JSONException {
    System.out.println("Parse JSON Array into Java Array");

    JSONArray jsonarray = new JSONArray(parse);

    int parselength = jsonarray.length();

    JSONObject arraynode ;

    String[] ID = new String[parselength];
    String[] Etag = new String[parselength];
    String[] City = new String[parselength];        
    String[] Address = new String[parselength];
    String[][] Links = new String[parselength][]; 
    //String[] Href = new String[parselength];
    //String[] Rel = new String[parselength];

    System.out.println("Json Array Length is: "+parselength);

    String strCSV = "";

    strCSV = "ID, Etag, City, Address, Href, Rel\r\n"; 
    for(int i=0; i<parselength; i++){

         arraynode = jsonarray.getJSONObject(i);

         ID[i] = arraynode.getString("Id");
         Etag[i] = arraynode.getString("ETag");
         City[i] = arraynode.getString("City");
         Address[i] = arraynode.getString("Address");

         //Href[i] = arraynode.getString("Href");
         //Rel[i] = arraynode.getString("Rel");

         JSONArray jsonarraylink = new JSONArray(arraynode.getJSONArray("Links").toString());
         int linklength = jsonarraylink.length();
         Links[i] = new String[linklength];
         for (int j = 0; j < linklength; j++) {
             arraynode = jsonarraylink.getJSONObject(i);
             Links[i][j] = arraynode.getString("Href");
             Links[i][j] = arraynode.getString("Rel");
             //Href[j] = arraynode.getString("Href");
             //Rel[j] = arraynode.getString("Rel");
             System.out.println(arraynode.getString("Href"));
             System.out.println(arraynode.getString("Rel"));
             strCSV += Etag[i]+","+ID[i]+","+City[i]+","+Address[i]+","+Links[i][j]+"\r\n";    // ,"+Href[j]+","+Rel[j]+"
         }                       
         //strCSV += Etag[i]+","+ID[i]+","+City[i]+","+Address[i]+"\r\n";    // ,"+Links[i]+"
    }       
    //CSV method is called with the string(strCSV) parameter. 
    Convertor(strCSV);
}