0

I have an ArrayList< Map < String, String > >, I want to write a method to create a JSON object from it. The object should look like this:

{
 "list":[ {"name": "john",
           "partner": "meredith",
           ...
          }
          {"name": "harry",
           ...
          }
          ...
        ]
}

The documentation of JSONSerializer.toJson(Object o) says o has to be a formatted Collection, but I couldn't find an example. How should I format my arraylist to be qualified as "formatted"?

Thanks!!

Rachel

4

1 回答 1

1

我不确定您是否研究过 Gson,或者您是否坚持自己这样做,但不久前有人向我指出 Gson,我发现它对于从甚至复杂的结构创建 json 字符串非常有效。它变得如此简单:

Gson gson = new Gson();
String json = gson.toJson(yourArrayList);

用户指南可以在这里找到https://sites.google.com/site/gson/gson-user-guide

如果这对您不起作用,请告诉我,我可以尝试帮助 JSONSerializer

于 2012-09-21T02:02:56.430 回答