0

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!

2
  • I've tried splitting the string and then extracting the imgurl. However the problem is that I have 50+ such strings and splitting all of them wastes a lot of resources. Thats why I need something more efficient. Commented Jul 11, 2012 at 6:31
  • 1
    @Sid, your string data look like a JSON string, so, you can choose some JSON library to extract imgurl or other variables. Commented Jul 11, 2012 at 6:34

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.