Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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
如何在App Engine中将json字符串反序列化为Java对象_Json_Google Cloud Platform_Gson_Google Http Client - Fatal编程技术网

如何在App Engine中将json字符串反序列化为Java对象

如何在App Engine中将json字符串反序列化为Java对象,json,google-cloud-platform,gson,google-http-client,Json,Google Cloud Platform,Gson,Google Http Client,我有一个UI,它发送这个对象- JSON格式的com.google.api.services.compute.model.Network,作为HTTP请求的一部分。我需要将这个对象反序列化为Java对象。我该怎么做?我写了一个简单的POJO来模仿这种行为 package com.techm.studio.client; import java.io.IOException; import com.google.api.services.compute.model.Network; impor

我有一个
UI
,它发送这个对象-
JSON
格式的
com.google.api.services.compute.model.Network
,作为
HTTP
请求的一部分。我需要将这个对象反序列化为
Java
对象。我该怎么做?我写了一个简单的
POJO
来模仿这种行为

package com.techm.studio.client;

import java.io.IOException;

import com.google.api.services.compute.model.Network;
import com.google.api.services.compute.model.NetworkRoutingConfig;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JSONTest {

    public static void main(String[] args) throws IOException {

        Network network = new Network();

        network.setName("k-network");
        network.setAutoCreateSubnetworks(false);
        network.setDescription("My test network on Google Cloud");
        network.setIPv4Range("10.0.0.0/16");
        network.setRoutingConfig(new NetworkRoutingConfig().setRoutingMode("REGIONAL"));

        String nwkJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(network);
        System.out.println("JSON String from POJO ==>\n" + nwkJsonString);

        Network netwrkOut = new Gson().fromJson(nwkJsonString, Network.class);
        System.out.println(netwrkOut.toPrettyString()); 
    }

}
这是我得到的输出

JSON String from POJO ==>
{
  "IPv4Range": "10.0.0.0/16",
  "autoCreateSubnetworks": false,
  "description": "My test network on Google Cloud",
  "name": "k-network",
  "routingConfig": {
    "routingMode": "REGIONAL"
  }
}
Exception in thread "main" java.lang.IllegalArgumentException: Can not set com.google.api.services.compute.model.NetworkRoutingConfig field com.google.api.services.compute.model.Network.routingConfig to com.google.gson.internal.LinkedTreeMap
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:764)
    at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:280)
    at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:241)
    at com.google.api.client.util.GenericData.put(GenericData.java:104)
    at com.google.api.client.util.GenericData.put(GenericData.java:48)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:188)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
    at com.google.gson.Gson.fromJson(Gson.java:887)
    at com.google.gson.Gson.fromJson(Gson.java:852)
    at com.google.gson.Gson.fromJson(Gson.java:801)
    at com.google.gson.Gson.fromJson(Gson.java:773)
    at com.techm.studio.client.JSONTest.main(JSONTest.java:25)

我不知道是否必须预先注册所有嵌套类型以及如何注册。

它看起来像
com.google.api.services.*
有自己的
com.google.api.client.json.
包,在本例中应该使用该包将模型序列化为
json
,并将
json
反序列化为模型。请参见以下示例:

import com.google.api.client.json.JsonGenerator;
import com.google.api.client.json.JsonParser;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.model.Network;
import com.google.api.services.compute.model.NetworkRoutingConfig;

import java.io.StringWriter;

public class GsonApp {

    public static void main(String[] args) throws Exception {
        Network network = new Network();

        network.setName("k-network");
        network.setAutoCreateSubnetworks(false);
        network.setDescription("My test network on Google Cloud");
        network.setIPv4Range("10.0.0.0/16");
        network.setRoutingConfig(new NetworkRoutingConfig().setRoutingMode("REGIONAL"));

        JacksonFactory jacksonFactory = new JacksonFactory();
        StringWriter writer = new StringWriter();
        try (JsonGenerator jsonGenerator = jacksonFactory.createJsonGenerator(writer)) {
            jsonGenerator.enablePrettyPrint();
            jsonGenerator.serialize(network);
        }

        String nwkJsonString = writer.toString();
        System.out.println("JSON String from POJO ==>\n" + nwkJsonString);

        JsonParser jsonParser = jacksonFactory.createJsonParser(nwkJsonString);
        Network netwrkOut = jsonParser.parse(Network.class);
        System.out.println(netwrkOut.toPrettyString());
    }
}
以上代码打印:

JSON String from POJO ==>
{
  "IPv4Range" : "10.0.0.0/16",
  "autoCreateSubnetworks" : false,
  "description" : "My test network on Google Cloud",
  "name" : "k-network",
  "routingConfig" : {
    "routingMode" : "REGIONAL"
  }
}
{
  "IPv4Range" : "10.0.0.0/16",
  "autoCreateSubnetworks" : false,
  "description" : "My test network on Google Cloud",
  "name" : "k-network",
  "routingConfig" : {
    "routingMode" : "REGIONAL"
  }
}
而且您不需要使用
Gson