如何在Java中将列表转换为Json

如何在Java中将列表转换为Json,java,json,Java,Json,如何在Java中将泛型列表转换为json。我有这样的类 public class Output { public int Keyname { get; set; } public Object outputvalue{ get; set; } //outvalue may be even a object collection } List<Output> outputList = new List<Output>(); 公共类输出 { publi

如何在Java中将泛型列表转换为json。我有这样的类

public class Output
{
    public int Keyname { get; set; }
    public Object  outputvalue{ get; set; }  //outvalue may be even a object collection
}

List<Output> outputList = new List<Output>();
公共类输出
{
public int Keyname{get;set;}
公共对象outputvalue{get;set;}//outvalue甚至可以是对象集合
}
List outputList=新列表();

我想将outputList转换成Java格式的json。转换后,我会将其发送给客户端。

您需要一个外部库来完成此操作

JSONArray jsonA = JSONArray.fromObject(mybeanList);
System.out.println(jsonA);
是这样的图书馆之一


您还可以查看将Java对象集合转换为JSON字符串的示例。

查看库。它提供了一个丰富的api来处理这个问题,并且使用起来非常简单。

使用GSON库。下面是示例代码

List<String> foo = new ArrayList<String>();
foo.add("A");
foo.add("B");
foo.add("C");

String json = new Gson().toJson(foo );
List foo=new ArrayList();
foo.添加(“A”);
foo.添加(“B”);
foo.添加(“C”);
字符串json=new Gson().toJson(foo);
下面是Gson的maven依赖项


com.google.code.gson

要向客户端发送Json,可以使用spring或在简单的servlet中添加以下代码

response.getWriter().write(json)

试试这个:

public void test(){
// net.sf.json.JSONObject, net.sf.json.JSONArray    

List objList = new ArrayList();
objList.add("obj1");
objList.add("obj2");
objList.add("obj3");
HashMap objMap = new HashMap();
objMap.put("key1", "value1");
objMap.put("key2", "value2");
objMap.put("key3", "value3");
System.out.println("JSONArray :: "+(JSONArray)JSONSerializer.toJSON(objList));
System.out.println("JSONObject :: "+(JSONObject)JSONSerializer.toJSON(objMap));
}

您可以找到API。

为了简单和结构良好,请使用SpringMVC。就这么简单

@RequestMapping("/carlist.json")
public @ResponseBody List<String> getCarList() {
    return carService.getAllCars();
}
@RequestMapping(“/carlist.json”)
public@ResponseBody List getCarList(){
return carService.getAllCars();
}

参考资料和信用证:

使用GSONBuilder和setPrettyPrinting,并禁用HTML以获得良好的输出

String json = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().
                            create().toJson(outputList  );
                    fileOut.println(json);

从Java2s下载java-json.jar 然后使用JSONArray构造函数

List myList = new ArrayList<>();    
JSONArray jsonArray = new JSONArray(myList);
System.out.println(jsonArray);
List myList=new ArrayList();
JSONArray JSONArray=新JSONArray(myList);
System.out.println(jsonArray);

jackson提供了非常有用的轻量级API,可以将对象转换为JSON,反之亦然。请查找下面的示例代码以执行该操作

List<Output> outputList = new ArrayList<Output>();
public static void main(String[] args) {
    try {
        Output output = new Output(1,"2342");
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(output);
        System.out.println(jsonString);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
}
List outputList=new ArrayList();
公共静态void main(字符串[]args){
试一试{
输出=新输出(1,“2342”);
ObjectMapper ObjectMapper=新的ObjectMapper();
字符串jsonString=objectMapper.writeValueAsString(输出);
System.out.println(jsonString);
}捕获(JsonProcessingException e){
e、 printStackTrace();
}
}
Jackson API还有许多其他特性和很好的文档。你可以参考如下链接:

要包含在项目中的依赖项包括

    <!-- Jackson -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.5.1</version>
    </dependency>

com.fasterxml.jackson.core
杰克逊数据绑定
2.5.1
com.fasterxml.jackson.core
杰克逊核心
2.5.1
com.fasterxml.jackson.core
杰克逊注释
2.5.1

如果你只知道outputvalue是一个
对象,那就相当困难了。我们甚至不能假设它是可序列化的。假设所有的类都是可序列化的。可能的重复您能告诉我们您在某个框架中使用了什么框架,例如spring或其他什么,它可以自动执行此操作,并将数据发送到客户端浏览器。@vmb-预期的输出是什么?@vbm-如果列表包含null,如何从json中删除null。输出是什么样子的?您应该编写
new ArrayList()
,而不是
new arrarylist()