如何从字符串中获取键和值

时间:2022-11-17 20:58:30

I have a question about how to write a regex expression to get the key and value pairs from below string?

我有一个关于如何编写正则表达式以从下面的字符串中获取键和值对的问题?

{"ReturnCode":"0","ErrorMsg":"success","EncryToken":"@@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

Could someone help me learn on what this can be done?

有人可以帮助我了解这可以做些什么吗?

Thanks very much.

非常感谢。

2 个解决方案

#1


3  

This one should suit your needs:

这个应该适合您的需求:

"([^"]+)"\s*:\s*"([^"]+)"

如何从字符串中获取键和值

The key is in the first group, the value in the second one.

键在第一组中,第二组中的值。

Visualization by Debuggex

Debuggex可视化

#2


1  

You can use Java JSON or the Google Gson to parse this JSON string ... as illustrated here - http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

您可以使用Java JSON或Google Gson来解析此JSON字符串...如此处所示 - http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

#1


3  

This one should suit your needs:

这个应该适合您的需求:

"([^"]+)"\s*:\s*"([^"]+)"

如何从字符串中获取键和值

The key is in the first group, the value in the second one.

键在第一组中,第二组中的值。

Visualization by Debuggex

Debuggex可视化

#2


1  

You can use Java JSON or the Google Gson to parse this JSON string ... as illustrated here - http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

您可以使用Java JSON或Google Gson来解析此JSON字符串...如此处所示 - http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/