0

I have string {"AAA":xxxxx,"BB":xxxx,"CCC":"3 xxx"}, and I want to get the values of AAA , BB and CCC as my output.I am using substring method of string i am able to get only AAA

metaDataValue = mettaDataValue.substring(metaDataValue.indexOf("")+1,metaDataValue.indexOf(":"));
5
  • Have you considered using String#split? Commented Apr 10, 2014 at 9:02
  • is this a json string?? If it is JSON then use JSON parser Commented Apr 10, 2014 at 9:03
  • AAA , BB , CC capture in my string, i am getting StringIndexOutOFBoundException when using while to traverse the string. Commented Apr 10, 2014 at 9:03
  • my string is store in this format {"AAA":xxxxx,"BB":xxxx,"CCC":"3 xxx"}, Commented Apr 10, 2014 at 9:05
  • So your String s="{"AAA":xxxx,"BB":xxx,"CCC":"3 xxx"}" is that so?? Commented Apr 10, 2014 at 9:07

5 Answers 5

2

If it is JSON use parser.

JSONObject json = new JSONObject();
json.getString("AAA");
Sign up to request clarification or add additional context in comments.

2 Comments

there is no JSON its just string in java
JSON is the format of the string - you parse the string into a JSON object.
0

Assuming a well-formed string, with no commas in the key and the value, a very simple solution can be:

String str = ...
str = str.substring(1, str.length() - 1);     // removing the {}
String[] entries = str.split(",");            // get all the "key":value
for (String entry: entries) {
   String[] entryData = entry.split(":");     // get the key and the value
   String key = entryData[0];
   key = key.substring(1, key.length() - 1);  // removing the "" from key
   String value = entryData[1];
   System.out.println(key + " -> " + value);
}

You can test here: http://ideone.com/weEbt2

If you want to have commas in keys or values, I think you have to make a little parser.

1 Comment

public void processString(String line) { metaDataValue = metaDataValue.substring(metaDataValue.indexOf("{")+1, metaDataValue.indexOf("}")); String [] splitMetaDataValue = metaDataValue.split(","); String [] tokenValue = null; for (int i = 0; i<splitMetaDataValue.length ; i++) { tokenValue = splitMetaDataValue[i].split(":"); System.out.println("Token Heads"+tokenValue[0]); } }//processString input String {"AAA":xxxx,"AAAB:xxxx,"AAAA":xxxx,} Token Heads"AAA" Token Heads"AAAB"
0

Use the Json Parser

Add the jar to your project

json: {"AAA":xxxxx,"BB":xxxx,"CCC":"3 xxx"}

Do like this to get the string in java

    JSONObject json=new JSONObject(string);
    String A=json.getString("AAA");
    String B=json.getString("BBB");
    String C=json.getString("CCC");

Comments

0

You can create class that contains properties AAA, BB and CCC

public class OutputResult {

    private String AAA;
    private String BB;
    private String CCC;

    // Getters and setters ...

And then read that object from json string, using for example Jackson:

ObjectMapper mapper = new ObjectMapper();
OutputResult outputResult = mapper.readValue(jsonString, OutputResult.class);

2 Comments

can u tell me how can i traverse in string??
First you can split string with str.split(","). Then you have array of expressions like this: "AAA":xxxx. Then you can split strings again with substring.split(":")
0

If your using only java,

you can use this.

    String str= "{\"AAA\":xxxxx,\"BB\":xxxx,\"CCC\":\"3 xxx\"}";
    String st[] = str.split("\"");
    for(int i=1;i<st.length-2;i+=2){
        System.out.println(st[i]);
    }

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.