Java json发布正文帮助,使用改型和jsonObject

Java json发布正文帮助,使用改型和jsonObject,java,json,retrofit2,Java,Json,Retrofit2,尽管这个json看起来很简单,但我无法将其正确格式化以在正文中发布 { "requests": [ { "action": "reject", "justification": "admin reason", "requestId": "ee4a5b4f3af54d849a63e305c13f6c8d" } ], "justification": "admin reas

尽管这个json看起来很简单,但我无法将其正确格式化以在正文中发布

{
    "requests": [
        {
            "action": "reject",
            "justification": "admin reason",
            "requestId": "ee4a5b4f3af54d849a63e305c13f6c8d"
        }
    ],
    "justification": "admin reason"
}
以下是我到目前为止在Android studio中使用的改进版2

Gson gson = new GsonBuilder()
    .setLenient()
    .create();
Retrofit.Builder builder = new Retrofit.Builder()
    .baseUrl("https://myurl.com/")
    .addConverterFactory(GsonConverterFactory.create(gson));
JsonObject jsonObject = new JsonObject();
JsonObject jsonObjectN = new JsonObject();
jsonObject.addProperty("action", action);
jsonObject.addProperty("request_id", request_id);
jsonObject.addProperty("justification", "blablabla");
jsonObjectN.add("requests", jsonObject);
jsonObjectN.addProperty("justification", justification);
Retrofit retrofit = builder.build();
Client client = retrofit.create(Client.class);
Call<PostRequest> user = client.postRequests(jsonObjectN);
非常接近,但我不太明白如何按照预期的精确数组/json格式制作它??
希望有人对Android和jsonObject足够了解,能够提供帮助

您没有添加json数组请求

尝试以下方法

Gson gson = new GsonBuilder()
    .setLenient()
    .create();
Retrofit.Builder builder = new Retrofit.Builder()
    .baseUrl("https://myurl.com/")
    .addConverterFactory(GsonConverterFactory.create(gson));
JsonObject jsonObject = new JsonObject();
JsonObject jsonObjectN = new JsonObject();
JsonArray jsonArray = new JsonArray();
jsonObject.addProperty("action", action);
jsonObject.addProperty("request_id", request_id);
jsonObject.addProperty("justification", "blablabla");
jsonArray.add(jsonObject);
jsonObjectN.add("requests", jsonArray);
jsonObjectN.addProperty("justification", justification);
Retrofit retrofit = builder.build();
Client client = retrofit.create(Client.class);
Call<PostRequest> user = client.postRequests(jsonObjectN);
Gson Gson=new GsonBuilder()
.setLenient()
.create();
reformation.Builder=新的reformation.Builder()
.baseUrl(“https://myurl.com/")
.addConverterFactory(GsonConverterFactory.create(gson));
JsonObject JsonObject=新的JsonObject();
JsonObject jsonObjectN=新的JsonObject();
JsonArray JsonArray=新的JsonArray();
jsonObject.addProperty(“动作”,动作);
addProperty(“请求id”,请求id);
jsonObject.addProperty(“理由”,“blablabla”);
add(jsonObject);
添加(“请求”,jsonArray);
jsonObjectN.addProperty(“对齐”,对齐);
改装改装=builder.build();
Client-Client=reformation.create(Client.class);
calluser=client.postRequests(jsonObjectN);

谢谢!!我找不到这样做的例子。所以现在我在获取com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:BEGIN_对象,但在第1行第2列路径$Im处是BEGIN_数组,假设它在响应的后面?我确实从邮递员那里得到了一个有效的帖子回复,并将其放入pojo中进行渲染,然后制作了一个类,并在回复时调用它,但我认为这肯定是不对的。我知道如果json不正确,api只响应[],如果是,那么它会响应真正的json响应
Request{method=POST, url=https://myurl.com/v1.0/approver/requests, tags={class retrofit2.Invocation=com.loginci.rgcislogin.Client.postRequests() [{"requests":{"action":"reject","request_id":"23423423refsdsdgdg","justification":"blablabla"},"justification":"Action done by Admin"}]}}
Gson gson = new GsonBuilder()
    .setLenient()
    .create();
Retrofit.Builder builder = new Retrofit.Builder()
    .baseUrl("https://myurl.com/")
    .addConverterFactory(GsonConverterFactory.create(gson));
JsonObject jsonObject = new JsonObject();
JsonObject jsonObjectN = new JsonObject();
JsonArray jsonArray = new JsonArray();
jsonObject.addProperty("action", action);
jsonObject.addProperty("request_id", request_id);
jsonObject.addProperty("justification", "blablabla");
jsonArray.add(jsonObject);
jsonObjectN.add("requests", jsonArray);
jsonObjectN.addProperty("justification", justification);
Retrofit retrofit = builder.build();
Client client = retrofit.create(Client.class);
Call<PostRequest> user = client.postRequests(jsonObjectN);