2015-01-12 91 views
-2

我想分析在javascript中由stringify()函數創建的java類(.java)中的json字符串。我知道解析字符串,如:如何解析JavaScript中stringify函數創建的json字符串?

   String JSON_DATA 
       = "{" 
       + " \"geodata\": [" 
       + " {" 
       + "  \"id\": \"1\"," 
       + "  \"name\": \"Julie Sherman\"," 
       + "  \"gender\" : \"female\"," 
       + "  \"latitude\" : \"37.33774833333334\"," 
       + "  \"longitude\" : \"-121.88670166666667\"" 
       + " }," 
       + " {" 
       + "  \"id\": \"2\"," 
       + "  \"name\": \"Johnny Depp\"," 
       + "  \"gender\" : \"male\"," 
       + "  \"latitude\" : \"37.336453\"," 
       + "  \"longitude\" : \"-121.884985\"" 
       + " }" 
       + " ]" 
       + "}"; 

但如何解析此字符串?

 var IO = { 
     //returns array with storable google.maps.Overlay-definitions 
     IN: function(arr, //array with google.maps.Overlays 
       encoded//boolean indicating whether pathes should be stored encoded 
       ) { 
      var shapes = [], 
        goo = google.maps, 
        shape, tmp; 

      for (var i = 0; i < arr.length; i++) 
      { 
       shape = arr[i]; 
       tmp = {type: this.t_(shape.type), id: shape.id || null}; 


       switch (tmp.type) { 
        case 'CIRCLE': 
         tmp.radius = shape.getRadius(); 
         tmp.geometry = this.p_(shape.getCenter()); 
         break; 
        case 'MARKER': 
         tmp.geometry = this.p_(shape.getPosition()); 
         break; 
        case 'RECTANGLE': 
         tmp.geometry = this.b_(shape.getBounds()); 
         break; 
        case 'POLYLINE': 
         tmp.geometry = this.l_(shape.getPath(), encoded); 
         break; 
        case 'POLYGON': 
         tmp.geometry = this.m_(shape.getPaths(), encoded); 

         break; 
       } 
       shapes.push(tmp); 
      } 

      return shapes; 
     } 

,並形成被解析字符串爲:

[{"type":"CIRCLE","id":null,"radius":1730.4622192451884,"geometry":[32.3610810916614,50.91339111328125]},{"type":"CIRCLE","id":null,"radius":1831.5495077322266,"geometry":[32.35528086804335,50.997161865234375]},{"type":"CIRCLE","id":null,"radius":1612.2461023303567,"geometry":[32.34454947365649,51.011924743652344]}] 
+1

你想解析JavaScript中的json字符串嗎?你的問題並不清楚 – neo

+0

你可以使用Gson來解析Java中的Json:https://sites.google.com/site/gson/gson-user-guide – DmitryKanunnikoff

+0

是的,我想解析java類中的字符串.java不是JavaScript的。 –

回答

0

您可以使用此GsonJackson。創建一個包含數據並使用這些庫的POJO。一個例如用GSON

import java.lang.reflect.Type; 
import java.util.List; 

import com.google.gson.Gson; 
import com.google.gson.reflect.TypeToken; 

class JsonData { 
    private String type; 
    private String id; 
    private double radius; 
    private List<Double> geometry; 

    //Getters & Setters 
} 

public class JsonParser { 

    public static void main(String[] args) { 
     String json = "[{\"type\":\"CIRCLE\",\"id\":null,\"radius\":1730.4622192451884,\"geometry\":[32.3610810916614,50.91339111328125]},{\"type\":\"CIRCLE\",\"id\":null,\"radius\":1831.5495077322266,\"geometry\":[32.35528086804335,50.997161865234375]},{\"type\":\"CIRCLE\",\"id\":null,\"radius\":1612.2461023303567,\"geometry\":[32.34454947365649,51.011924743652344]}]"; 

     Type listType = new TypeToken<List<JsonData>>() {}.getType(); 
     List<JsonData> disputeSummaryArraylistobjectList = new Gson().fromJson(json, listType); 
     System.out.println(disputeSummaryArraylistobjectList); 
    } 

} 
0

你需要像GSONJackson一個Java的JSON解析器。

有解析兩種策略:

  1. 創建Java對象,讓輸入的JSON解析器地圖要素領域
  2. 遍歷通用JSON數據結構解析器返回

這兩個項目的文檔包含大量的例子,如何實現。