如何反序列化JSON中的某些字段?

时间:2022-08-23 08:10:40

I'm using Gson to extraxt some fields. By the way I don't want to create a class due to the fact that I need only one value in all JSON response. Here's my response:

我正在使用Gson来提取某些字段。顺便说一下,由于我在所有JSON响应中只需要一个值,因此我不想创建一个类。这是我的回答:

{
    "result": {
        "name1": "value1",
        "name2": "value2",
    },
    "wantedName": "wantedValue"
}

I need wantedValue but I don't want to create the entire class for deserialization. Is it possible to achieve this using Gson?

我需要wantValue但我不想为反序列化创建整个类。是否有可能使用Gson实现这一目标?

3 个解决方案

#1


4  

If you need one field only, use JSONObject.

如果只需要一个字段,请使用JSONObject。

import org.json.JSONException;
import org.json.JSONObject;


public class Main { 
public static void main(String[] args) throws JSONException  {

    String str = "{" + 
            "    \"result\": {" + 
            "        \"name1\": \"value1\"," + 
            "        \"name2\": \"value2\"," + 
            "    }," + 
            "    \"wantedName\": \"wantedValue\"" + 
            "}";

    JSONObject jsonObject = new JSONObject(str);

    System.out.println(jsonObject.getString("wantedName"));
}

Output:

输出:

wantedValue

#2


1  

If you don't have to use Gson, I would use https://github.com/douglascrockford/JSON-java. You can easily extract single fields. I could not find a way to do something this simple using Gson.

如果您不必使用Gson,我会使用https://github.com/douglascrockford/JSON-java。您可以轻松提取单个字段。我用Gson找不到这么简单的方法。

You would just do

你会做的

String wantedName = new JSONObject(jsonString).getString("wantedName");

#3


0  

Can use just a portion of gson, using it just to parse the json:

可以只使用gson的一部分,用它来解析json:

Reader reader = /* create reader from source */
Streams.parse(new JsonReader(reader)).getAsJsonObject().get("wantedValue").getAsString();

#1


4  

If you need one field only, use JSONObject.

如果只需要一个字段,请使用JSONObject。

import org.json.JSONException;
import org.json.JSONObject;


public class Main { 
public static void main(String[] args) throws JSONException  {

    String str = "{" + 
            "    \"result\": {" + 
            "        \"name1\": \"value1\"," + 
            "        \"name2\": \"value2\"," + 
            "    }," + 
            "    \"wantedName\": \"wantedValue\"" + 
            "}";

    JSONObject jsonObject = new JSONObject(str);

    System.out.println(jsonObject.getString("wantedName"));
}

Output:

输出:

wantedValue

#2


1  

If you don't have to use Gson, I would use https://github.com/douglascrockford/JSON-java. You can easily extract single fields. I could not find a way to do something this simple using Gson.

如果您不必使用Gson,我会使用https://github.com/douglascrockford/JSON-java。您可以轻松提取单个字段。我用Gson找不到这么简单的方法。

You would just do

你会做的

String wantedName = new JSONObject(jsonString).getString("wantedName");

#3


0  

Can use just a portion of gson, using it just to parse the json:

可以只使用gson的一部分,用它来解析json:

Reader reader = /* create reader from source */
Streams.parse(new JsonReader(reader)).getAsJsonObject().get("wantedValue").getAsString();