Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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
如何在Java中建模Json数组?_Java_Json_Microsoft Graph Api - Fatal编程技术网

如何在Java中建模Json数组?

如何在Java中建模Json数组?,java,json,microsoft-graph-api,Java,Json,Microsoft Graph Api,我对Java还很陌生。 我想将此请求建模为对Microsoft graphAPI的批处理请求 {"requests":[ {"id":"employeeId","method":"GET","url":"/me/employeeId"}, {"id":"thumbnailPhoto","method&

我对Java还很陌生。 我想将此请求建模为对Microsoft graphAPI的批处理请求

{"requests":[
  {"id":"employeeId","method":"GET","url":"/me/employeeId"},
  {"id":"thumbnailPhoto","method":"GET","url":"/me/photo/$value"}]
}
所以“requests”是BatchRequest对象的数组

我目前拥有的:

// BatchRequest object
public class BatchRequest
{
  private String id;

  private String method;

  private String url;

  public BatchRequest(String id, String method, String url)
  {
    this.id = id;
    this.method = method;
    this.url = url;
  }
  
  // getters and setters below
}
私有列表请求;
@凌驾
公共用户信息响应callGraphApi()
{
BatchRequest employeeId=新的BatchRequest(“employeeId”、“GET”、“me/employeeId”);
BatchRequest photo=新的BatchRequest(“thumbnailPhoto”、“GET”、“me/photo/$value”);
请求。添加(employeeId);
请求。添加(照片);
返回callGraphApi(请求);
}

这就是我建模JSON的方式吗?

当然。将一个简单java对象的实例转换成一组JSON的任务,以及将一组JSON与一组简单java类组合成这些类的实例的任务,称为“编组”。你需要一个图书馆来做这件事;流行的是和。

在我试图找出如何在android应用程序开发中将Json响应建模为java对象时发现了这一点。在您的项目中安装gsonjackson,它将处理引擎盖下的事情

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class BatchRequest {

    @SerializedName("requests")
    @Expose
    private List<Request> requests = null;

    public List<Request> getRequests() {
        return requests;
    }

    public void setRequests(List<Request> requests) {
        this.requests = requests;
    }

}

我正在使用Dropwizard框架,所以我想Jackson会在幕后处理事情。这就是正确的做法。谢谢,那个网站真的很有帮助。我在用杰克逊。这就是我在我的主要方法中使用它的方式。1)我创建了一个新的请求对象,2)将其添加到列表requestList。第三)BatchRequest requests=新的BatchRequest(requestList)。
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class BatchRequest {

    @SerializedName("requests")
    @Expose
    private List<Request> requests = null;

    public List<Request> getRequests() {
        return requests;
    }

    public void setRequests(List<Request> requests) {
        this.requests = requests;
    }

}
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Request {
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("method")
    @Expose
    private String method;
    @SerializedName("url")
    @Expose
    private String url;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}