-5

我有一个包含以下内容的文本文件

{"sno":"1c8d1d7eaa32ff3f58a8822111276d5a","at":"app","tt":{"AppParam1":"AppParamValue1","AppParam2":"AppParamValue2"}},"c":{"dt":"Microsoft XDeviceEmulator","pn":"WP","pv":"8.0.9832.0"}

在java中,我想找到'sno','pn'和'pv'并替换(我的意思是)当前上述文本文件的值

"sno" has value "1c8d1d7eaa32ff3f58a8822111276d5a"
"pn" has value "WP"
"pv" has value "8.0.9832.0"

新价值观

"sno" has to be changed to "637829"
"pn"  has to be changed to "XYZ"
"pv" has to be changed to "2.2.4.0"

非常感谢您的帮助 !谢谢

4

2 回答 2

1

那是一个 JSON 对象。

将该字符串转换为 JSON 对象,更改值,然后再次将其转换为字符串。

在这里您可以阅读 Java 中的 JSON 对象:http: //www.json.org/java/

这里有一个关于如何使用它们的简单教程:http: //answers.oreilly.com/topic/257-how-to-parse-json-in-java/

于 2012-09-19T12:44:31.410 回答
0

您可以将其转换为 JSON,进行更改,然后将其转换回字符串。或者如果你真的想自己替换这些值,你可以使用这些行:

yourstring = yourstring.replaceFirst("\"sno\":\"[^\"]+\"", "\"sno\":\"637829\"");
yourstring = yourstring.replaceFirst("\"pn\":\"[^\"]+\"", "\"pn\":\"XYZ\"");
yourstring = yourstring.replaceFirst("\"pv\":\"[^\"]+\"", "\"pv\":\"2.2.4.0\"");

仅当无法在字符串的其他位置找到这些键时

于 2012-09-19T12:46:10.193 回答