1

I have a string like this {"product_tags":"yin_yang,yin yang"}. what I want to do is just avoid everything else other than yin yang. There is two strings but I just want the first one.

Note that in some cases even if the second string is not available I want to get the same result. And that string might change so it is not necessary that the string will be always yin_yang sometimes it can be motorbike or anything else.

6
  • please note in some cases even if the second string is not available i want to get the same result. and that string might change so it is not necessary that the string will be always yin_yang sometimes it can be motorbike or anything else. Commented Feb 28, 2014 at 6:42
  • This is not clear, you want some method to transform {"product_tags":"xxxxxxxx,yin yang"} into "xxxxxxxx"? Commented Feb 28, 2014 at 6:45
  • you want first string or second string after : Commented Feb 28, 2014 at 6:46
  • yes you are currect i want the first which is xxxxxxxx Commented Feb 28, 2014 at 6:47
  • and this string can be {"product_tags":"yin_yang"} as well? Commented Feb 28, 2014 at 6:55

2 Answers 2

1

It Look like JSON String Use the JSONParser in java

JSONObject jobject=new JSONObject(STRING);
String value=jobject.getString("product_tags");

EDITED

Using REGEX

 String json="{\"product_tags\":\"yin_yang,yin yang\"}";
    json=json.replaceAll("([{}]+)", "");
    String value[]=json.split(":");
    System.out.print(value[1]);
Sign up to request clarification or add additional context in comments.

4 Comments

sorry mate its not a json string and its not a json array.. its just a value inside one column in a database. and i want to get that value from database but need to avoid all other strings other than the one coming after :
@sarath yes, that' ok. the new JSONObject(<your string>) will create the JSONObject() to typecast it.
@sarath you can get like that value using this code.regex may not be accurate.
@sarath I have Updated my answer using regex
0

You can use StringTokenizer to parse your string

String str ="{\"product_tags\":\"yin_yang,yin yang\"}";
StringTokenizer to = new StringTokenizer(str,":}");
while(to.hasMoreTokens()){
    String firstString = (String) to.nextElement();
    String secondString = (String) to.nextElement();
System.out.print(secondString);
}

1 Comment

secondString is not accessible out size the while loop;

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.