2

I got a String:

["4fd1cf1783353a15415","4ffecf87fcc40d110a965626"]

or

["4fd5f684815345","4fd6ef3e60a676854651","4fd83c33c19164512153"]

And I'd like to store every id (eg. 4fd5f684815345, 4fd6ef3e60a676854651, 4fd83c33c19164512153...) in a independant String (or ArrayList).

How to parse it, because the String can be dynamic (1,2,3 values or more)?

OR JSON Array Parsing:

My JSON

"idCards":[
"4fc52f95egvt418541515",
"4fd1d05454151541545115"
],

A part of my code:

msg3 = (JSONArray) myobject.get("idCards");
System.out.println(msg3.toJSONString()); 

The result:

[4fc52f95egvt418541515","4fd1d05454151541545115"]

I'd like this 2 values in 2 differents String.

Many thanks for your help!

5
  • You might wanna try regex, you can use delimiter to split 'em up. I am not quite sure about the code though. :) Commented Jul 20, 2012 at 9:45
  • Please clarify your question. You have a String array in that format? You have string data coming from a file or socket in that format? with the [ and ] and double-quotes? Commented Jul 20, 2012 at 9:47
  • 2
    This is a JSON data structure. The correct way to parse this is to use a JSON library. Commented Jul 20, 2012 at 9:47
  • Thanks for your reply. I added some clarifications. Commented Jul 20, 2012 at 9:54
  • You converted from a JSON String into a JsonArray, the next step is to convert this to a Java List. And you're done. I have modified my answer to show you. Commented Jul 20, 2012 at 9:57

6 Answers 6

4

It would appear to be that this could be a JSON String. In which case, you may make use of a Java JSON Library to help you parse that into Java native objects.

http://www.json.org/

http://code.google.com/p/google-gson/

String data = "[\"4fd5f684815345\",\"4fd6ef3e60a676854651\",\"4fd83c33c19164512153\"]";

// parse JSON String to JSON Array
JsonArray array = (JsonArray) (new JsonParser()).parse(data);

// build a Java ArrayList
List<String> stringList = new ArrayList<String>();

// for each item in JsonArray, add to Java ArrayList
for (int i = 0; i < array.size(); i++) {
    stringList.add((array.get(i)).getAsString());
}
Sign up to request clarification or add additional context in comments.

Comments

2

I fully agree with the JSON answers, but if this is a one-off hack, you could just do this:

    String input = "[\"4fd5f684815345\",\"4fd6ef3e60a676854651\",\"4fd83c33c19164512153\"]";
    input = input.replace("[", "");
    input = input.replace("]", "");
    input = input.replace("\"", "");
    String[] parts = input.split(",");

1 Comment

String.replace also replaces all occurrences so no need to use replaceAll.
2

I make a number of assumptions here:

  • Assume no spaces before and after the delimiting [, ], ,
  • Assume no , and " character in the Strings you want to extract

    input.substring(1, input.length() - 1).replaceAll("\\\"", "").split(",");
    

    Or if you don't want to mess with regular expression (replaceAll function works with regular expression), then you can use replace method:

    input.substring(1, input.length() - 1).replace("\"", "").split(",");
    

Due to the assumptions above, this answer is very brittle. Consider using JSON parser if the data is really JSON.

6 Comments

@dacwe: Taken care of by the substring. Need to update the answer with assumption.
Too fast, sorry, however, use replace instead of replaceAll when replacing non-regex.
@dacwe: I really don't know: Can you remove character with replace?
Yes you can, check it out.
@dacwe: It is likely that it is implemented with Pattern class, though. OpenJDK does grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/…
|
1
    String str = "[\"4fd5f684815345\",\"4fd6ef3e60a676854651\",\"4fd83c33c19164512153\"]";
    String strArry[] = null;

    if(str.trim().length() > 0){
        str = str.substring(1 , str.length()-1).replaceAll("\\\"", "");
        strArry = str.split(",");
    }

Comments

0

If s is the input string, it can just be as simple as

String[] idArray = s.replaceAll("[\\[\\]\"]", "").split(",");

Comments

0

it would be more secure (because ',' may be a decimal separator) to split with ("\",\""), and not remove trailing " in replaceAll, here subtring do not parse all the string :

final String str = "[\"4fd5f684815345\",\"4fd6ef3e60a676854651\",\"4fd83c33c19164512153\"]";

final String strArray[] = str.substring(2, str.length() - 2).split("\",\"");
final ArrayList<String> al = new ArrayList<String>();

for (final String string : strArray) {
    al.add(string);
    System.out.println(string);
}
System.out.println(al);    

for (final String string : strArray) { System.out.println(string); }

Output :

4fd5f684815345
4fd6ef3e60a676854651
4fd83c33c19164512153
[4fd5f684815345, 4fd6ef3e60a676854651, 4fd83c33c19164512153]

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.