简体   繁体   English

泽西岛(JAVA)中的服务器GET JSON

[英]Server GET json in jersey, JAVA

im not sure how to get a json object and output it in jersey using rest GET from a ajax json post, im using grizzly server, server has been set, heres the code that supposed to get the json, correct me please, thanks! 我不确定如何获取一个json对象,并使用ajax json帖子中的rest GET在jersey中将其输出到球衣中,我使用grizzly服务器,服务器已设置,这是应该获取json的代码,请纠正我,谢谢!

import java.io.IOException;
import java.io.InputStream;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.io.IOUtils;

import javax.ws.rs.*;

@Path("/helloworld")
public class GetData {
    @GET
    @Consumes("application/json")
            public String getResource(JSONObject obj) throws IOException {

        InputStream in = (InputStream) obj.values();
        String data = IOUtils.toString(in);

        JSONObject out = (JSONObject) JSONSerializer.toJSON(data);

        String result = out.getString("name");
        return result;       


    }

} 

You need to find out, what is your JSON object should be deserialized to. 您需要找出应该反序列化的JSON对象是什么。 If it's just a JSONObject and you want to parse it manually: 如果它只是一个JSONObject,而您想手动解析它:

@Consumes("application/json")
public String getResource(JSONObject obj) {
...
}

If it is some kind of custom object: 如果它是某种自定义对象:

@Consumes("application/json")
public String getResource(CustomObj customObj) {
...
}

But then you should take care about marshalling/unmarshalling of that object to JSON by Jackson. 但是随后,您应该注意Jackson将该对象编组/解编为JSON的注意事项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM