简体   繁体   中英

Convert JSON key value using RegExp

I receive a JSON string from a server call, in this form:

{"0":{"jpgN":"2","spread_value":"392.22","relevant_new":"text"},"1":{"jpgN":"1","spread_value":"395.28","relevant_new":"text"},"count":2}

Using RegExp, is there any way to replace the value of any key jpgN with the string "http://mydomain.com/keyValue.jpg" (eg "http://mydomain.com/2.jpg" )?

Replace "jpgN":"([^"]+)" to "jpgN":"http://mydomain.com/$1.jpg" .

But it's better to parse json and change values using your programming language.

Something like that ?

var json = {"0":{"jpgN":"2","spread_value":"392.22","relevant_new":"text"},"1":{"jpgN":"1","spread_value":"395.28","relevant_new":"text"},"count":2};
var s = JSON.stringify(json);

s.replace(/\"jpgN\":\"(\w+)\"/g, "\"jpgN\":\"http://mydomain.com/$1.jpg\"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM