【问题标题】:Parse JSON(with subJson) without java subobjects解析没有 java 子对象的 JSON(with subJson)
【发布时间】:2026-02-12 23:25:01
【问题描述】:

我如何解析这样的 json 字符串:

{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  }
}



但是,从 "address" 我只需要 "city"。我如何在不创建新类(对于 lib.GSON)的情况下做到这一点?
我尝试使用 JsonPath,但我无法理解如何将 JsonObject“地址”替换为字符串值“城市”。

【问题讨论】:

  • 您能否澄清您的问题,我有点困惑:),您只需要来自addresscity 或者想用city 替换完整的address 对象??跨度>
  • 您不需要将字符串地址替换为城市。你只需要解析JSON树结构
  • @Rishaldevsingh 我找到了解析所有 json 的简单方法,但我不需要“地址”中的所有值。我只需要“firstName”、“lastName”、“age”和“city”
  • 是的,对于城市,您首先必须指向地址,然后在地址对象中您会找到城市字段
  • 试试这个:JsonPath.read(document, "$.address.city");请参阅以下链接了解更多详情。 github.com/json-path/JsonPath

标签: java json parsing gson jsonpath


【解决方案1】:

给你

try {
        JSONObject jsonObject=new JSONObject(jsonString);
        JSONObject address=jsonObject.getJSONObject("address");
        String city=address.getString("city");
    } catch (JSONException e) {
        e.printStackTrace();
    }

【讨论】:

  • 是的)我知道我可以像这样)但使用 lib。 GSON 我可以让它更容易。但是对于 GSON 我需要创建新的 POJO,我不想要它
【解决方案2】:

试试类似的东西

DocumentContext ctx = JsonPath.parse("your-json-here");
YourPojoHere pojo = new YourPojoHere(
   ctx.read("$.firstName"),
   ctx.read("$.lastName"),
   ctx.read("$.age"),
   ctx.read("$.address.city"));

【讨论】:

    【解决方案3】:

    我想我理解你的问题是正确的:你想用/address/city 替换/address,这样

    {
      "firstName": "John",
      "lastName" : "doe",
      "age"      : 26,
      "address"  : {
        "streetAddress": "naist street",
        "city"         : "Nara",
        "postalCode"   : "630-0192"
      }
    }
    

    然后是

    {
      "firstName": "John",
      "lastName" : "doe",
      "age"      : 26,
      "address"  : "Nara"
    }
    

    选项 A:您可以使用 it.bewares JSON,然后使用 JSON 选择器就可以了:

    JSONFactory JSON = new JSONFactory(SimpleJSONParser.class, SimpleJSONGnerator.class);
    
    String input = "{\"firstName\":\"John\",\"lastName\":\"doe\",\"age\":26,\"address\":{\"streetAddress\":\"naist street\",\"city\":\"Nara\",\"postalCode\":\"630-0192\"}}";
    JSONValue json = JSON.parse(input);
    
    json.put("address", JSON.find(new JSONSelector(".\"address\".\"city\"")))
    

    您可以对 JSONFactory 使用依赖注入:

    @Inject
    JSONFactory JSON;
    

    选项 B:相反,您可以与 it.bewares JSON 以及 it.bewares JSONPatch 结合使用。 JSON Patch 是一个提议的标准(参见WikipediaRFC 6902

    一个有效的 JSON 补丁应该是:

    [
      { "op": "move", "from": "/address/city", "path": "/address" }
    ]
    

    使用 it.bewares JSONPatch 它会是:

    JSONFactory JSON = new JSONFactory(SimpleJSONParser.class, SimpleJSONGnerator.class);
    
    String input = "{\"firstName\":\"John\",\"lastName\":\"doe\",\"age\":26,\"address\":{\"streetAddress\":\"naist street\",\"city\":\"Nara\",\"postalCode\":\"630-0192\"}}";
    JSONValue json = JSON.parse(input);
    
    String patchString = "[{\"op\":\"move\",\"from\":\"/address/city\",\"path\":\"/address\"}]";
    JSONPatch patch = new JSONPatch(JSONArray<JSONObject<JSONString>> JSON.parse(patchString));
    JSONPatch.execute(patch, json);
    

    【讨论】: