lets say I have a String like this
[{ "name" : "Ronald" , "firstname" : "Ruck"} , { "name" : "Yunchin" , "firstname" : "Cha"} , { "name" : "Klaus" , "firstname" : "Mixer"}]
Sometimes the string is shorter/longer, important is that at start there is [{ and at the end }]. I would like to read just the name and firstname, put a name + firstname to one String and then put every single Name+Firstname-String in an array. Ok sounds weird? If I would iterate my new array with these names there should be an output: Ronald Ruck, Yunchin Cha, Klaus Mixer… Any ideas? I know something with regex but im not good at it. Thank you!
Edit:
Yes it looks like a Json. But if Im using a json reader like http://code.google.com/p/json-simple/ I get an error: Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject At first I tought its because there is an “[” at the start and a “]” at the end. Without that I get: Unexpected token COMMA(,) at position 44. So its maybe not a real json? I could try http://jackson.codehaus.org/ too, but im not sure that will work.
Edit2:
Ok everything is fine 🙂 With json-simple its working, just have to use the JSONArray correctly. The solution with the Gson Lib is ok too, thank you all.
The string you provided is a valid json string. You can check the validity here. You can use Gson library to parse json string in java.
Read this Tutorial first than my code will make more sense to you.
Now, the corresponding
Getter and Setter classfor the above code:Output =