从输入的 JSON 中获取一个 JsonNode,使用 Java 8。

huangapple go评论74阅读模式
英文:

Retrieve get a jsonNode form the input json in java8

问题

以下是翻译好的内容:

private String retrievePayload(String payloadToRetrieve, String path) {
    // 在这里执行你的操作来定位和移除指定路径上的节点
    return 最终的JSON字符串;
}

你需要编写代码来正确地定位和移除指定路径上的节点。这个代码部分需要你自己实现,我无法为你提供完整的解决方案。你可以使用 JsonPath 库,但需要编写适当的代码来检查节点是否在正确的路径上,并将其从 JSON 中移除。

希望这能帮助你朝正确的方向前进。如果你有具体的问题或需要进一步的帮助,请随时提出。

英文:

I have he following jsonObject:

<!-- language: json -->

{
  &quot;payloadTesting&quot;: {
	&quot;A&quot;: &quot;a&quot;,
	&quot;B&quot;: 21,
	&quot;node that I want to remove&quot;: {
		&quot;C&quot;: &quot;123&quot;,
		&quot;D&quot;: &quot;456&quot;
	}
  }
}

What I want to to is to go to the location of the 'node that I want to remove' and remove it from the original payload,so my final json would be:

<!-- language: json -->

{
  &quot;payloadTesting&quot;: {
    &quot;A&quot;: &quot;a&quot;,
    &quot;B&quot;: 21
  }
}

To do that I searched for the JsonPath library and tryied to use it like this:

<!-- language: java -->

private String retrievePayload(String payloadToRetrieve, String path) {
    return JsonPath
            .using(Configuration.builder().jsonProvider(new JsonOrgJsonProvider())
                    .options(Option.REQUIRE_PROPERTIES).build())
            .parse(enrichedFieldName)
            .jsonString();
}

But that solution only returns me the name 'node that I want to retrieve' and not the payload without that node. And this does not verify if that same node is on the correct path or not, because I only what to remove that node if it is on the correct path.

For example, I only want to remove the node if it is on the root ("$"). If it was on the $.A, for example, I dont want to remove it.

How can I do this correctly?

答案1

得分: 1

这里有两种方法可以做到:

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;

public class test1 {
    
    public static void main(String[] args) {
        Gson gson = new Gson();
        
        
        String request = "{\"payloadTesting\": {" +
                "\"A\": \"a\"," +
                "\"B\": 21," +
                "\"node that I want to remove\": {" +
                "\"C\": \"123\"," +
                "\"D\": \"456\"" +
                "}" +
                "}" +
                "}";
        // method 1 using google Gson
        
        JsonObject json = gson.fromJson(request, JsonObject.class);
        json.getAsJsonObject("payloadTesting").remove("node that I want to remove");
        System.out.println("deleted using gson:" + json);
         
        
        // method 2 using library com.jayway.jsonpath.DocumentContext 
        
        DocumentContext doc = JsonPath.parse(request);
        String  jsonPath = "$.payloadTesting[\"node that I want to remove\"]";
        doc.delete(jsonPath);
        String JsonAfterDelete = doc.jsonString();
        System.out.println("deleted using pathway:" + JsonAfterDelete);
    
    }

}
英文:

I know it's late but maybe helpful for others, here I have used 2 ways to do it

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;

public class test1 {
	
	public static void main(String[] args) {
		Gson gson = new Gson();
		
		
		String request = &quot;{\r\n&quot; + 
				&quot;  \&quot;payloadTesting\&quot;: {\r\n&quot; + 
				&quot;    \&quot;A\&quot;: \&quot;a\&quot;,\r\n&quot; + 
				&quot;    \&quot;B\&quot;: 21,\r\n&quot; + 
				&quot;    \&quot;node that I want to remove\&quot;: {\r\n&quot; + 
				&quot;        \&quot;C\&quot;: \&quot;123\&quot;,\r\n&quot; + 
				&quot;        \&quot;D\&quot;: \&quot;456\&quot;\r\n&quot; + 
				&quot;    }\r\n&quot; + 
				&quot;  }\r\n&quot; + 
				&quot;}&quot;;
		// method 1 using google Gson
		
		  JsonObject json = gson.fromJson(request, JsonObject.class);
		  json.getAsJsonObject(&quot;payloadTesting&quot;).remove(&quot;node that I want to remove&quot;);
		  System.out.println(&quot;deleted using gson:&quot;+json);
		 
		
		// method 2 using library com.jayway.jsonpath.DocumentContext 
		
		DocumentContext doc = JsonPath.parse(request);
		String  jsonPath=&quot;$.payloadTesting[\&quot;node that I want to remove\&quot;]&quot;;
		doc.delete(jsonPath);
		String JsonAfterDelete = doc.jsonString();
		System.out.println(&quot;deleted using pathway:&quot;+JsonAfterDelete);
	
	}

}

huangapple
  • 本文由 发表于 2020年4月3日 20:09:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61011618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定