1

私はこのMAPのような地図を持っています:

{ 
  facility-1={
    facility-kind1={param1=XPath-1, param2=XPath-2}, 
    facility-kind2={param1=XPath-1, param2=XPath-2}, 
    facility-kind3={param1=XPath-1, param2=XPath-2}
  },
  facility-2={
    facility-kind1={param1=XPath-1, param2=XPath-2}, 
    facility-kind2={param1=XPath-1, param2=XPath-2}, 
    facility-kind3={param1=XPath-1, param2=XPath-2}
  }
}

このようにフォーマットされたJSONに変換したい

[

    {"title": "Item 1"},
    {"title": "Folder 2",
        "children": [
            {"title": "Sub-item 2.1"},
            {"title": "Sub-item 2.2"}
        ]
    },
    {"title": "Folder 3",
        "children": [
            {"title": "Sub-item 3.1"},
            {"title": "Sub-item 3.2"}
        ]
    },
    {"title": "Item 5"}
]

GSON を使用しようとしましたが、結果の出力は私が望んでいたものではありませんでした:

{
  "facility-1": {
     "facility-kind1":
      {"param1":"XPath-1","param2":"XPath-2"},
     "facility-kind2":
      {"param1":"XPath-1","param2":"XPath-2"},
     "facility-kind3":
      {"param1":"XPath-1","param2":"XPath-2"}
  },
  "facility-2": { 
     "facility-kind1":
      {"param1":"XPath-1","param2":"XPath-2"},
     "facility-kind2":
      {"param1":"XPath-1","param2":"XPath-2","param3":"XPath-3"},
     "facility-kind3":
      {"param1":"XPath-1","param2":"XPath-2"}
  }
}

必要に応じてフォーマットされたjsonを取得するにはどうすればよいですか??

4

2 に答える 2

1

JSON を提供した新しい形式に変換する必要があります。

変換するデータ:

static String json = 
    "{\n" + 
    "  facility-1={\n" + 
    "    facility-kind1={param1=XPath-1, param2=XPath-2},\n" +  
    "    facility-kind2={param1=XPath-1, param2=XPath-2},\n" + 
    "    facility-kind3={param1=XPath-1, param2=XPath-2}\n" + 
    "  },\n" + 
    "  facility-2={\n" + 
    "    facility-kind1={param1=XPath-1, param2=XPath-2},\n" +  
    "    facility-kind2={param1=XPath-1, param2=XPath-2},\n" +  
    "    facility-kind3={param1=XPath-1, param2=XPath-2}\n" + 
    "  }\n" + 
    "}\n";

GSON の使用

データを処理するいくつかのクラスを作成します。それらは次のようになります。

static class Facility {
    List<Kind> children = new LinkedList<Kind>();
}

static class Kind {
    String title;
    Map<String, String> params;

    public Kind(String title, Map<String, String> params) {
        this.title = title;
        this.params = params;
    }
}

次のステップは、ソースを見て、その表現を作成することです。私は使うだろう:

Map<String, Map<String, Map<String, String>>>

入力データがそのようにレイアウトされているためです。nowを使用して変換するGsonのは非常に簡単です。

public static void main(String... args) throws Exception {

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    Type type = new TypeToken<
            Map<String, Map<String, Map<String, String>>>>() {}.getType();

    Map<String, Map<String, Map<String, String>>> source = 
        gson.fromJson(json, type);

    Map<String, Facility> dest = new HashMap<String, Facility>();

    for (String facilityName : source.keySet()) {
        Map<String, Map<String, String>> facility = source.get(facilityName);

        Facility f = new Facility();

        for (String kindName : facility.keySet())
            f.children.add(new Kind(kindName, facility.get(kindName)));

        dest.put(facilityName, f);
    }

    System.out.println(gson.toJson(dest));
}

JSONObject/JSONArray の使用

public static void main(String... args) throws Exception {

    JSONObject source = new JSONObject(json);
    JSONArray destination = new JSONArray();

    for (Iterator<?> keys = source.keys(); keys.hasNext(); ) {

        String facilityName = (String) keys.next();
        JSONObject kinds = source.getJSONObject(facilityName);

        JSONArray children = new JSONArray();
        for (Iterator<?> kit = kinds.keys(); kit.hasNext(); ) {

            String kind = (String) kit.next();
            JSONObject params = kinds.getJSONObject(kind);

            JSONObject kindObject = new JSONObject();
            kindObject.put("title", kind);

            for (Iterator<?> pit = params.keys(); pit.hasNext(); ) {
                String param = (String) pit.next();
                kindObject.put(param, params.get(param));
            }
            children.put(kindObject);
        }

        JSONObject facility = new JSONObject();
        facility.put("title", facilityName);
        facility.put("children", children);
        destination.put(facility);
    }
    System.out.println(destination.toString(2));
}
于 2012-06-07T06:55:40.303 に答える
0

あなたが欲しいのはきれいな印刷です。

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(yourMap);
于 2012-06-07T04:58:24.910 に答える