Monday, 15 June 2015

Parsing 2D array in JSON using Java -


I have a json file with the following format. I am having trouble parsing the "Location" segment, which is a value of a double [] [] array, what is the best way to parse it? Also, I do not understand how to read the file, I, json.simple but it is required to cast every JSONObject or JSONArray used which is inconvenient to use a little bit

  [{"id":. 123456, "Location": [[37.785220, -122.4,04,378], [37.786661, -122.4,04,571], [37.7, 86, 356, -122.48, 99 2] "," Time ": 1426510324," test failed ": [" T1 "," T2 "," T5 "]}, {" id ": 123456 , "Location": [[37.793450, -122.422616], [37.78286 9], -122.4, 62, 613], [37.772, 964, -122.45857 9], [37] 7, 62787, -122.458, 922]], "Time": +1426510325, "Failed in Test": ["T3", "T5", "T6"]}, {"id" : 123456, "location": [[37.778689, -122.5,14,214], [37.782,75 9, -122.5, 11, 63], [37.8] , 05,187, -122.4,68, 9 24], [37.771611, -122.4,68,66 ], [37.7.5 9, 05, 9, -122.47736]], "time": 1426511324, "test failed": ["T1", "T5", "T7"]}]    

you double [] [] as < The / p array can be simplified using

  string input = "[\ r \ n" + "\" id \ ": 123456, \ r \ n" + "\" location \ ": [\ R \ n "+" [37.785220, - 122.404378], \ r \ n "+" [37.786, 661, -122.4,04,571], \ r \ n "+" [37] .7,86,356, -122.4,08, 99 2] \ r \ n "+"], \ r \ n "+" \ "time \": 1426510324, \ r \ n "+" \ "trial Failed \ ": [\" T1 \ ", \" T2 \ ", \" T5 \ "] \ r \ n" + "}]"; // Parse the input as an array JSONArray jArr = new JSONArray (input); // get the required place object JSONObject jObj1 = jArr.getJSONObject (0); // Get location string location = jObj1.getJSONArray ("location"). ToString (); // Use paras GSO Double [] [] DAR = New GSON () J. Jason (location, double [] []. Square); // Print to the array (double [] d: dArr) {System.out.println (Arrays.toString (d)); }  

Output:

  [37.78522, -122.404378] [37.786661, -122.404571] [37.786356, -122.408 992]  

No comments:

Post a Comment