Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 如何从JSON中提取返回两个相同JSON字符串的字段_Java_Json_String - Fatal编程技术网

Java 如何从JSON中提取返回两个相同JSON字符串的字段

Java 如何从JSON中提取返回两个相同JSON字符串的字段,java,json,string,Java,Json,String,我有一个JSON看起来像这样 { "description": { "html": "A remote code execution vulnerability exists in the way that the scripting engine handles objects in memory in Microsoft Edge. ...", "text": "<p>A remote code execution vulnerab

我有一个JSON看起来像这样

{
    "description":
    {
        "html": "A remote code execution vulnerability exists in the way that the scripting engine handles objects in memory in Microsoft Edge. ...",
        "text": "<p>A remote code execution vulnerability exists in the way that the scripting engine handles objects in memory in Microsoft Edge. ...</p>"
    }
}

有没有办法只提取文本内容?

将描述提取为JSON对象,而不是提取为字符串。
你的代码是这样的

JSONObject json3 = json2.getJSONObject("description")
那么

另外,一个快速提醒,我正在使用

Edit1:因为您使用的是simple.json

JSONObject json2 = (JSONObject) object.get("description");
String html = (String) json2.get("html");
String text = (String) json2.get("text");

最好的方法是使用JsonPath了解更多工作示例

好吧,因为它是嵌套的json,类似于
json2.get(“description”).get(“text”)
应该可以做到这一点(假设
json2
重复一个映射、JsonObject等)-您可能需要添加一些解析和一些空检查,但我将把它们留给您。
json2
的类型是什么?有许多不同的JSON解析器/库提供不同的API,所以答案将取决于您使用的是什么。我使用的是SimpleJSON库,我添加了完整的代码,我没有mond get(“文本”),除了主要问题:似乎您的
文本和
html
被翻转,由于
text
包含
..

结构,而
html
只包含“呈现”数据。这是从服务器端返回的-我想它们也被翻转了,我不想使用其他库!JSONObject json2=(JSONObject)object.get(“description”),对象未知。
String html = json3.get("html")
String text = json3.get("text")
JSONObject json2 = (JSONObject) object.get("description");
String html = (String) json2.get("html");
String text = (String) json2.get("text");