在Java中使用Jayway解析同时包含转义和非转义字符的JSON。

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

Parse JSON having both Escape & Unescape characters in JAVA using Jayway

问题

我将翻译以下JSON数据部分:

{
    "shopping": {
        "cart": {
            "items": [{
                "iturl": "https://www.google.com/",
                "itdesc": "Item’s box includes the below contents:\n a.adaptor \n b.sdfd"
            }]
        }
    }
}

请注意,上述JSON数据中的"已被正确翻译为双引号(")以表示JSON字符串中的引号。

英文:

I receive the below json as an input to my program:

{
    "shopping": {
        "cart": {
            "items": [{
				"iturl" : "https://www.google.com/",
				"itdesc" : "Item’s box includes the below contents:\n a.adaptor \n b.sdfd"
			}]
		}
	}
}

We are using jayway jsonpath to parse this data and do some processing and return the final value as a string.

when we parse it with the default jsonpath configuration, I get the iturl modified as "https:\/\/www.google.com\/"

Tried changing the JSONProvider to JacksonJsonProvider (by referring https://stackoverflow.com/questions/34111276/jsonpath-with-jackson-or-gson) and the issue with the url is solved but, the value of itdesc is now coming to new line (due to \n) making it an invalid json.

I cannot specifically handle for each field as the incoming data will be dynamic.

Is there any proper way to parse this kind of JSON in java. Thanks in advance for your help

答案1

得分: 1

尝试在解析字符串之前再添加一个转义级别,字符串解析器将为"\\n"提供"\n"。

例如,使用Jackson ObjectMapper进行解析。

objectMapper.readValue(jsonString.replace("\\\\", "\\\\\\"), Any.class);
英文:

Try adding one more escaping level before parsing the string, the string parser's gonna give you "\n" for "\\n".

For example, parsing with Jackson ObjectMapper.

objectMapper.readValue(jsonString.replace("\\", "\\\\"), Any.class);

答案2

得分: -1

{
"shopping": {
"cart": {
"items": [
{
"iturl": "https://www.google.com/",
"itdesc": "商品包含以下内容:\n a. 适配器 \n b. sdfd"
}
]
}
}
}

private void readData() {
String Body = (response json string from connection);
JSONParser parse = new JSONParser();
String iturl = null;
String itdesc = null;

try  {
    JSONObject shopping =  (JSONObject) parse.parse(Body);
    JSONObject cart=  (JSONObject) shopping.get("cart");
    JSONArray  items = (JSONArray  ) cart.get("items ");
    items.forEach((k)-> {
        JSONObject inside = (JSONObject) k;
        iturl = inside.get("iturl");
        itdesc = inside.get("itdesc");
    });
}catch ( ParseException e) {
    e.printStackTrace();
}

}

if this come from file.json combine with reader :

private static final File jsonData = new File(file.json);
private void callData() {
String iturl = null;
String itdesc = null;
try {
Reader reader = new FileReader(marketList);
JSONParser parse = new JSONParser();
JSONObject shopping = (JSONObject) parse.parse(reader);
JSONObject cart= (JSONObject) shopping.get("cart");
JSONArray items = (JSONArray ) cart.get("items ");
items.forEach((k)-> {
JSONObject inside = (JSONObject) k;
iturl = inside.get("iturl");
itdesc = inside.get("itdesc");
});
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}

英文:
{
"shopping": { <-- JSONObject
    "cart": { <-- JSONObject
        "items": [{ <-- JSONArray
            "iturl" : "https://www.google.com/", <-- JSONObject inside JSONAray
            "itdesc" : "Item’s box includes the below contents:\n a.adaptor \n b.sdfd"
        }]
    }
}

}

if this data json come from http connection.
this json must be a string format fisrt,
and try using org.json.simple
so do like this :

private void readData() {
    String Body = (response json string from connection);
    JSONParser parse = new JSONParser();
    String iturl = null;
    String itdesc = null;

    try  {
        JSONObject shopping =  (JSONObject) parse.parse(Body);
        JSONObject cart=  (JSONObject) shopping.get("cart");
        JSONArray  items = (JSONArray  ) cart.get("items ");
        items.forEach((k)-> {
            JSONObject inside = (JSONObject) k;
            iturl = inside.get("iturl");
            itdesc = inside.get("itdesc");
        });
    }catch ( ParseException e) {
        e.printStackTrace();
    }

}

if this come from file.json combine with reader :

private static final File jsonData = new File(file.json);
private void callData() {
    String iturl = null;
    String itdesc = null;
    try  {
        Reader reader = new FileReader(marketList);
        JSONParser parse = new JSONParser();
        JSONObject shopping =  (JSONObject) parse.parse(reader);
        JSONObject cart=  (JSONObject) shopping.get("cart");
        JSONArray  items = (JSONArray  ) cart.get("items ");
        items.forEach((k)-> {
            JSONObject inside = (JSONObject) k;
            iturl = inside.get("iturl");
            itdesc = inside.get("itdesc");
        });
    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
}

huangapple
  • 本文由 发表于 2020年7月22日 13:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63027621.html
匿名

发表评论

匿名网友

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

确定