我有一个包含以下内容的文本文件
{"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"
非常感谢您的帮助! 感谢
答案 0 :(得分:1)
这是一个JSON对象。
将该字符串转换为JSON对象,更改值,然后再将其转换为字符串。
在这里,您可以阅读java中的JSON对象:http://www.json.org/java/
这里有一个关于如何使用它们的简单tuto:http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/
答案 1 :(得分:0)
您可以将其转换为JSON,进行更改,然后将其转换回字符串。 或者,如果您真的想自己替换这些值,可以使用以下行:
yourstring = yourstring.replaceFirst("\"sno\":\"[^\"]+\"", "\"sno\":\"637829\"");
yourstring = yourstring.replaceFirst("\"pn\":\"[^\"]+\"", "\"pn\":\"XYZ\"");
yourstring = yourstring.replaceFirst("\"pv\":\"[^\"]+\"", "\"pv\":\"2.2.4.0\"");
只有在字符串
的其他地方找不到这些密钥的时候