简体   繁体   中英

java complicated string parsing

I need the most efficient way to parse the following string and extract the imgurl from it using java.

{ms:"images,5160.1",
turl:"http://ts1.mm.bing.net/th?id=I4693880201938488&pid=1.1",
height:"178",width:"300",
imgurl:"http://www.attackingsoccer.com/wp-content/uploads/2011/07/World-Cup-2012-Draw.jpg",
offset:"0",t:"World Cup 2014 Qualification – Europe Draw World Cup 2012 Draw ...",
w:"719",h:"427",ff:"jpeg",
fs:"52",durl:"www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe...",
surl:"http://www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe-draw/world-cup-2012-draw/",
mid:"D9E91A0BA6F9E4C65C82452E2A5604BAC8744F1B",k:"6",ns:"API.images"}"

For the above string the output should be :

http://www.attackingsoccer.com/wp-content/uploads/2011/07/World-Cup-2012-Draw.jpg

Any help is appreciated.

Thanks!

Seems like it's JSON message. You can convert this into POJO using eg GSON.

This is in JSON format. Find a parser that handles JSON data and go from there.

String str = YOUR STRING;
String startStr = "imgurl:¥"";
String endStr = ".jpg";

String value = str.subString(str.indexOf(startStr) + startStr.length , str.indexOf(endStr) + endStr.length);

You can also use a Pattern and a Matcher :

String string = "something....,imgurl=\"blabla\",somethinother";
Pattern p = Pattern.compile(",imgurl=\"[^\"]*\",");
Matcher m = p.matcher(string);
m.find();
String result = m.group().subSequence(1, m.group().length() - 1).toString();

This will work even if there are other places in the string where the text "imgurl:" or ".jpg" appears.

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