英文:
Convert a string (having escape characters, new line chars) to plain string
问题
String str = "[{\\n \\"country_group\\": [\\"Asia\\"],\\n \\"country\\": [\\n \\"IND\\" , \\"China\\"\\n ]\\n },\\n {\\n \\"country_group\\": [\\"NAmerica\\"],\\n \\"country\\": [\\n \\"US\\"\\n ]\\n }]";
str = str.replace("\\\\"", "\"")
.replace("\\\\n", "\n")
.replace("\\\\t", "\t");
System.out.println(str);
英文:
I have a string :
String str = "[{\\n \\\"country_group\\\": [\\\"Asia\\\"],\\n \\\"country\\\": [\\n \\\"IND\\\" , \\\"China\\\"\\n ]\\n },\\n {\\n \\\"country_group\\\": [\\\"NAmerica\\\"],\\n \\\"country\\\": [\\n \\\"US\\\"\\n ]\\n }]";
On Sysout it gives me :
[{\n \"country_group\": [\"Asia\"],\n \"country\": [\n \"IND\" , \"China\"\n ]\n },\n {\n \"country_group\": [\"NAmerica\"],\n \"country\": [\n \"US\"\n ]\n }]
I want to convert it to a plain string which on sysout will give me below output :
[{
"country_group": ["Asia"],
"country": [
"IND" , "China"
]
},
{
"country_group": ["NAmerica"],
"country": [
"US"
]
}]
My ultimate objective is to convert it to a JSONObject. If I get this above output, then I will be able to do so.
Edit: I am seeking a function which will directly convert this. without using any regex.
答案1
得分: 2
首先,我会处理你自己的评论。我认为这是不合适的,特别是因为你可能可以找到很多关于给定问题的内容,请给社区一些时间来看到你的问题并回答它。
关于给定问题,正如你已经注意到的,你在处理 JSON 输入。在处理这类问题时,最好使用第三方库,这样你就会得到关照,并且能够节省很多时间。
我对这种情况的建议是使用 Gson 或 JSONOrg 库。这些库用于将 JSON 读取为一个“普通”字符串,这样你就可以对其进行操作、修改或其他任何操作。但我们遇到了另一个问题,就是我们需要“取消转义”字符串。幸运的是,这也可以通过一个库来解决,在这种情况下是 Apache Commons。因此,“取消转义”字符串的代码如下所示。
StringEscapeUtils.unescapeJson(str)
既然我们已经解决了这个问题,我们就可以对 JSON 进行处理了。
public class TestClass {
public String convertWithGson(String jsonInput) {
GsonBuilder gsonBuilder;
Gson gson;
gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gson = gsonBuilder.create();
CountryTest[] countryTest = gson.fromJson(jsonInput, CountryTest[].class);
/*
对对象进行你喜欢的操作
*/
return gson.toJson(countryTest);
}
public String convertWithJSONOrg(String jsonInput) {
JSONTokener jsonTokener = new JSONTokener(jsonInput);
JSONArray jsonArray = new JSONArray(jsonTokener);
return jsonArray.toString(2);
}
}
如果你希望映射对象并在 Java 中轻松操作它,Gson 更适合。CountryTest 类如下:
public class CountryTest {
private List<String> country_group;
private List<String> country;
}
当然要有所有的 getter 和 setter 方法。
运行代码如下:
public static void main(String[] args) {
TestClass testClass = new TestClass();
String str = "[{\\n \\"country_group\\": [\\"Asia\\"],\\n \\"country\\": [\\n \\"IND\\" , \\"China\\"\\n ]\\n },\\n {\\n \\"country_group\\": [\\"NAmerica\\"],\\n \\"country\\": [\\n \\"US\\"\\n ]\\n }]";
System.out.println(testClass.convertWithGson(StringEscapeUtils.unescapeJson(str)));
System.out.println("-----------");
System.out.println(testClass.convertWithJSONOrg(StringEscapeUtils.unescapeJson(str)));
}
输出如下:
[
{
"country_group": [
"Asia"
],
"country": [
"IND",
"China"
]
},
{
"country_group": [
"NAmerica"
],
"country": [
"US"
]
}
]
-----------
[
{
"country": [
"IND",
"China"
],
"country_group": ["Asia"]
},
{
"country": ["US"],
"country_group": ["NAmerica"]
}
]
希望我能帮上忙,而且请放慢脚步,编程非常有趣,但不是我们想要匆忙完成的事情。它总会回到我们身边。
英文:
firstly I would address your own comment. I think it's inappropriate, especially since you could probably find a lot of content for the given problem, and please do give the community time to see your question and answer it.
As for the given problem, as you've noticed already, you are dealing with a JSON input. When dealing with something such as this, it's best to use a third-party library, so you are taken care of and you save up a lot of time on it.
My recommendation for this situation is to use Gson or JSONOrg libraries. Now, those the libraries for reading JSON as a "normal" String, so you can play with it, modify it, or anything you like. But we encounter another problem, it is that we have to "unescape" the String. Fortunately, it is also solvable with a library, Apache commons in this case. So the code for "unescaping" the string will look like this.
StringEscapeUtils.unescapeJson(str)
Now that we've got that out of the way, we can process the JSON if we wish.
public class TestClass {
public String convertWithGson(String jsonInput) {
GsonBuilder gsonBuilder;
Gson gson;
gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gson = gsonBuilder.create();
CountryTest[] countryTest = gson.fromJson(jsonInput, CountryTest[].class);
/*
Do with the object what you like
*/
return gson.toJson(countryTest);
}
public String convertWithJSONOrg(String jsonInput) {
JSONTokener jsonTokener = new JSONTokener(jsonInput);
JSONArray jsonArray = new JSONArray(jsonTokener);
return jsonArray.toString(2);
}
}
Gson is more suitable if you wish to map the objects and manipulate it with Java easily.
The CountryTest class:
public class CountryTest {
private List<String> country_group;
private List<String> country;
}
With all the getters and setters of course.
Running it would look like:
public static void main(String[] args) {
TestClass testClass = new TestClass();
String str = "[{\\n \\\"country_group\\\": [\\\"Asia\\\"],\\n \\\"country\\\": [\\n \\\"IND\\\" , \\\"China\\\"\\n ]\\n },\\n {\\n \\\"country_group\\\": [\\\"NAmerica\\\"],\\n \\\"country\\\": [\\n \\\"US\\\"\\n ]\\n }]";
System.out.println(testClass.convertWithGson(StringEscapeUtils.unescapeJson(str)));
System.out.println("-----------");
System.out.println(testClass.convertWithJSONOrg(StringEscapeUtils.unescapeJson(str)));
}
With an output:
[
{
"country_group": [
"Asia"
],
"country": [
"IND",
"China"
]
},
{
"country_group": [
"NAmerica"
],
"country": [
"US"
]
}
]
-----------
[
{
"country": [
"IND",
"China"
],
"country_group": ["Asia"]
},
{
"country": ["US"],
"country_group": ["NAmerica"]
}
]
Process finished with exit code 0
I hope I have been of help, and please, do take it slow, programming is very fun, but not something we want to rush. It always comes back to us.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论