Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/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
Java GSON库中的自定义读写方法_Java_Json_Gson - Fatal编程技术网

Java GSON库中的自定义读写方法

Java GSON库中的自定义读写方法,java,json,gson,Java,Json,Gson,嗨,我有以下几个问题 { “名称”:“testapp2”, “类型”:“Web”, “packageName”:“test2.com”, “秘密”:{“0987654321”、“91287465827”}, “otpExpiry”:“1800”, “senderId”:“验证”, “otpLength”:“4”, “requestPerIp”:“500”, “签名”:“#OTP是您的验证码” } 我正在编写我的自定义JSON读写器这是我的代码覆盖了GSON TypeAdapter类这是我的代码

嗨,我有以下几个问题


{
“名称”:“testapp2”,
“类型”:“Web”,
“packageName”:“test2.com”,
“秘密”:{“0987654321”、“91287465827”},
“otpExpiry”:“1800”,
“senderId”:“验证”,
“otpLength”:“4”,
“requestPerIp”:“500”,
“签名”:“#OTP是您的验证码”
}

我正在编写我的自定义JSON读写器这是我的代码覆盖了GSON TypeAdapter类这是我的代码。我正在尝试捕获我的JSON对象并将其转换为我的自定义模型类。。。基本上我想解析嵌套的json对象

public Application read(JsonReader reader) throws IOException {
Application application = new Application();
String secKey=null;
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())
       secKey+=reader.nextString();
      application.setSecretKey(secKey);
      break;
    case "signature":
      application.setSignature(reader.nextString());
      break;
    case "sendOTPInResponse":
      application.setSendOTPInResponse(reader.nextInt());
      break;
    default:
      reader.skipValue();
      break;
  }
}
reader.endObject();
return application;

此方法无法分析。。请帮助…

请使用GSON库,它提供了从JSONString到您的模型的转换

 private Store getStoreModel(String jsonStore) {

    Type type = new TypeToken<Store>() {
    }.getType();
    Store store = new Gson().fromJson(jsonStore, type);
    return store;
您需要创建模型并传递jsonString

在这里您可以创建modelclass

public class Store  {
public String StoreId;
public String StoreDisplayLink;
public String StoreAddress;
}

GSON库将把JSONString映射到模型中

 private Store getStoreModel(String jsonStore) {

    Type type = new TypeToken<Store>() {
    }.getType();
    Store store = new Gson().fromJson(jsonStore, type);
    return store;
私有存储getStoreModel(字符串jsonStore){
Type Type=new-TypeToken(){
}.getType();
Store Store=new Gson().fromJson(jsonStore,type);
退货店;
}

有关更多信息,请访问该链接

  • 首先,JSON无效:
    secretKey
    声明为对象,而不是数组
  • 其次,您忘记了在
    案例“secretKey”
    中关闭数组:
    reader.endArray()
  • 第三,
    secKey
    变量被声明为
    null
    ,因此它必须是一个空字符串
    ,以便不将前导的
    “null”
    标记添加到
    secretKey
    字段
但是,自定义流读取非常适合处理庞大的数据集,但您最好使用简单的反序列化策略,并将繁琐的反序列化工作直接委托给Gson:

比如说,反序列化方法定义如下:

静态应用程序反序列化应用程序(最终JsonReader阅读器){
返回gson.fromJson(reader,ApplicationIncomingDto.class).application();
}
这里的一个特例是
ApplicationType
,它作为字符串存储在JSON中,但需要转换为
ApplicationType
。因此,应使用
GsonBuilder
,以便:

private static final Gson Gson=new GsonBuilder()
.registerTypeAdapter(ApplicationType.class,(JsonDeserializer)(json,typeOfT,context)->新的ApplicationType(json.getAsJsonPrimitive().getAsString())
.create();
您可以在其中绑定
ApplicationType
类和符合您需要的自定义
JsonDeserializer
实现。下面的代码片段声明了一个定制的数据传输对象,该对象专门为数据反序列化而设计,然后转换为
应用程序
类。但是,您可以直接反序列化到
应用程序
,但通常这不是一个好主意(比如,您可以使用类似Gson的注释注释DTO类字段;或者切换到另一个JSON库,并保持
应用程序
类不变)

私有静态最终类应用程序incomingdto{
私有最终字符串名称=null;
private final ApplicationType=null;
私有最终字符串packageName=null;
私有最终整数otpExpiry=null;
私有最终字符串senderId=null;
私有最终整数otpLength=null;
private final Long requestPerIp=null;
私有最终列表secretKey=null;
私有最终字符串签名=null;
私有应用程序(){
最终应用程序=新应用程序();
application.setName(name);
应用程序.setType(type);
application.setPackageName(packageName);
application.setOtpExpiry(otpExpiry);
应用程序。setSenderId(senderId);
应用程序。设置增压器(增压器);
application.setRequestPerIp(requestPerIp);
application.setSecretKey(secretKey.stream().collect(collector.joining());
申请书、签字(签字);
退货申请;
}
}
最后,它的使用方式:

out.println(反序列化应用程序(新JsonReader(新StringReader(JSON)));
输出(考虑到
toString()
对于
Application
ApplicationType
都被覆盖):

应用程序{type=ApplicationType{name='Web'},packageName='test2.com',otpExpiry=1800,name='testapp2',senderId='VERIFY',otpLength=4,requestPerIp=500,secretKey='098765432191287465827',签名='#OTP是您的验证码',sendOTPInResponse=0}


“secretKey”:{“0987654321”,“91287465827”}“这不是数组。[“0987654321”,“91287465827”]我改成了这个,但不工作非常有用