Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将ArrayList的内容显示为格式正确的JSON for Java REST API_Java_Json_Api_Arraylist_Jersey - Fatal编程技术网

将ArrayList的内容显示为格式正确的JSON for Java REST API

将ArrayList的内容显示为格式正确的JSON for Java REST API,java,json,api,arraylist,jersey,Java,Json,Api,Arraylist,Jersey,我正在构建一个Restful API,它将以JSON格式发送响应。我要发送的数据在arrayList中。这样做的简单方法似乎是将arrayList转换为JSON,因为有一个内置的toJSONString(),但该调用会将其转换为一个带有大量正斜杠的JSON强函数,例如:{“value1\”:95、“value2\”:“275\”}。我想将其转换为适合RESTAPI的普通JSON字符串,例如:{“value1”:“30”、“value2”、“65”}。有没有可能做到这一点?您只需在返回ArrayL

我正在构建一个Restful API,它将以JSON格式发送响应。我要发送的数据在arrayList中。这样做的简单方法似乎是将arrayList转换为JSON,因为有一个内置的toJSONString(),但该调用会将其转换为一个带有大量正斜杠的JSON强函数,例如:{“value1\”:95、“value2\”:“275\”}。我想将其转换为适合RESTAPI的普通JSON字符串,例如:{“value1”:“30”、“value2”、“65”}。有没有可能做到这一点?

您只需在返回ArrayList的方法上方使用注释“@products(MediaType.APPLICATION_JSON)”

例如:

假设我们有一个模型类“Message”: 文件名:Message.java

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Message {

    private long id;
    private String message;
    private Date created;
    private String author;

    public Message(){

    }

    public Message(long id, String message, String author){
        this.id = id;
        this.message = message;
        this.author = author;
        this.created = new Date();
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

}
然后我们有一个MessageService: 文件名:MessageService.java

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MessageService {

    private Map<Long, Message> messages;

    public MessageService(){
        messages.put(1L, new Message(1, "Hello Stackoverflow", "Kevin"));
        messages.put(2L, new Message(2, "Hello Jersey", "Kevin"));
    }

    public List<Message> getAllMessages(){
        return new ArrayList<Message>(messages.values());
    }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Map;
公共类消息服务{
私人地图信息;
公共信息服务{
消息。放置(1L,新消息(1,“你好,Stackoverflow”,“Kevin”));
消息。放置(2L,新消息(2,“你好,泽西”,“凯文”));
}
公共列表getAllMessages(){
返回新的ArrayList(messages.values());
}
}
最后,您将拥有一个资源文件,其中实际包含返回ArrayList的方法: 文件名:MessageResource.java

@Path("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Message> getMessages(){

        return messageService.getAllMessages();
        //This method will return your ArrayList as JSON.
    }
}
@Path(“/messages”)
公共类消息资源{
MessageService=newmessageservice();
@得到
@产生(MediaType.APPLICATION_JSON)
公共列表getMessages(){
return messageService.getAllMessages();
//此方法将以JSON的形式返回ArrayList。
}
}
然后只需从URL调用MessageResource类:

注意:localhost:8080/kevinswebapi是您的根路径


我希望这有帮助……

很大程度上取决于您用于RESTful服务的库或框架。正如@Makoto所说,通常您使用的框架将处理列表转换为适当的JSON数组。看起来您已经序列化了两次列表。我们正在使用jersey库。请显示生成此列表的代码。