你好我试图删除这个字符串周围的双引号(虽然这是我在Java中创建的JsonArray对象但是这给出了我想省略的“”)
"secretKey":"[\"121212\",\"32234324\"]"
我想要的是这个
"secretKey" : ["121212","32234324"]
在我的JsonAdapter(实现TypeAdapter)中reader.beginArray()
抛出异常
IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 179 path $.secretKey
帮助我,自从过去两天以来我被困住了。
以下是一些代码,如果你们都能理解我实际上是想通过makeiteasy对象构建器创建一个对象。
public class ApplicationWrapperMaker {
public static final Property<ApplicationWrapper, Long> id = newProperty();
public static final Property<ApplicationWrapper, User> createdBy = newProperty();
public static final Property<ApplicationWrapper, Integer> updatedBy = newProperty();
public static final Property<ApplicationWrapper, Timestamp> createdAt = newProperty();
public static final Property<ApplicationWrapper, String> packageName = newProperty();
public static final Property<ApplicationWrapper, Integer> otpExpiry = newProperty();
public static final Property<ApplicationWrapper, Integer> otpLength = newProperty();
public static final Property<ApplicationWrapper, Integer> requestPerIp = newProperty();
public static final Property<ApplicationWrapper, String> senderId = newProperty();
public static final Property<ApplicationWrapper, String> key = newProperty();
public static final Property<ApplicationWrapper, String> name = newProperty();
public static final Property<ApplicationWrapper, String> signature = newProperty();
public static final Property<ApplicationWrapper, ApplicationType> type = newProperty();
public static final Property<ApplicationWrapper, JSONArray> secretKey = newProperty();
public static final Instantiator<ApplicationWrapper> ApplicationWrapper = lookup -> {
ApplicationWrapper applicationWrapper = new ApplicationWrapper();
// List<String> secKeys = new ArrayList<String>();
// secKeys.add("some-sec-key");
// secKeys.add("some-other-sec-key");
applicationWrapper.setPackageName(lookup.valueOf(packageName, "some.package.name"));
applicationWrapper.setOtpExpiry(lookup.valueOf(otpExpiry, (Integer) null));
applicationWrapper.setKey(lookup.valueOf(key, "some-application-key"));
applicationWrapper.setName(lookup.valueOf(name, "someApplicationName"));
applicationWrapper.setSenderId(lookup.valueOf(senderId, "somesenderId"));
applicationWrapper.setCreatedBy(lookup.valueOf(createdBy, make(a(UserMaker.User))));
applicationWrapper.setCreatedAt(lookup.valueOf(createdAt, CommonUtils.getMysqlTimeStamp()));
applicationWrapper.setType(lookup.valueOf(type, make(a(ApplicationTypeMaker.ApplicationType))));
applicationWrapper.setUpdatedBy(lookup.valueOf(updatedBy, 11));
applicationWrapper.setOtpLength(lookup.valueOf(otpLength, 6));
applicationWrapper.setSignature(lookup.valueOf(signature, "some-signature,#OTP"));
applicationWrapper.setRequestPerIp(lookup.valueOf(requestPerIp, 100));
applicationWrapper.setSecretKey(lookup.valueOf(secretKey, new JSONArray(make(a(ApplicationSecretKeyMaker.ApplicationSecretKey)))));
// System.out.println(applicationWrapper.getSecretKeys().toString());
// Gson gson = new Gson();
// System.out.println(gson.toJson(applicationWrapper));
// System.out.println(new JSONArray(applicationWrapper.getSecretKeys()));
return applicationWrapper;
//
};
并且该对象被赋予GSON读写方法
public class ApplicationJsonAdapter extends TypeAdapter<ApplicationWrapper> {
@Override
public void write(JsonWriter out, ApplicationWrapper application) throws IOException {
out.beginObject();
out.name("name").value(application.getName());
out.name("packageName").value(application.getPackageName());
out.name("otpExpiry").value(application.getOtpExpiry());
out.name("type").value(application.getType().getName());
out.name("key").value(application.getKey());
out.name("senderId").value(application.getSenderId());
out.name("otpLength").value(application.getOtpLength());
out.name("requestPerIp").value(application.getRequestPerIp());
out.name("secretKey").value(application.getSecretKey().toString());
out.name("signature").value(application.getSignature());
out.name("sendOTPInResponse").value(application.getSendOTPInResponse());
out.endObject();
}
@Override
public ApplicationWrapper read(JsonReader reader) throws IOException {
ApplicationWrapper application = new ApplicationWrapper();
JSONArray secretKeys=new JSONArray();
JSONObject obj = new JSONObject();
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
switch (name) {
case "type":
ApplicationType type = new ApplicationType(reader.nextString());
application.setType(type);
break;
case "packageName":
application.setPackageName(reader.nextString());
break;
case "otpExpiry":
application.setOtpExpiry(reader.nextInt());
break;
case "name":
application.setName(reader.nextString());
break;
case "senderId":
application.setSenderId(reader.nextString());
break;
case "otpLength":
application.setOtpLength(reader.nextInt());
break;
case "requestPerIp":
application.setRequestPerIp(reader.nextLong());
break;
case "secretKey":
reader.beginArray();
while (reader.hasNext()) {
secretKeys.put(reader.nextString());
}
System.out.println("json adapter" +secretKeys);
reader.endObject();
application.setSecretKey(secretKeys);
break;
case "signature":
application.setSignature(reader.nextString());
break;
case "sendOTPInResponse":
application.setSendOTPInResponse(reader.nextInt());
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
return application;
}
}
这是有问题的一行
reader.beginArray();
编辑:当我通过REST客户端使用JSON并使用API时,所有工作都可以使用
提前致谢!!!
答案 0 :(得分:1)
out.name("secretKey").value(application.getSecretKey().toString());
以上一行是错误的。你将它变成了一个字符串而不是一个数组,从而使你的读者失败。你似乎想要:
out.name("secretKey")
out.beginArray();
for ( final String s : application.getSecretKey() ) {
out.value(s);
}
out.endArray();
一旦您从JSONArray
媒体资源中删除secretKey
,就会将其更改为String[]
或List<String>
。另外,我强烈建议您使用数据绑定而不是简单的流式传输。
P.S。 Gson领域中不存在JSONArray
。 + JSON相关的东西应仅用于序列化和反序列化目的。
答案 1 :(得分:0)
这不是有效的JSON
尝试: {&#34; secretKey&#34; :[&#34; 121212&#34;,&#34; 32234324&#34;] }