1

I have a JSON/string/array, not sure what it is now as it’s been through a spinner and is now in a String variable, it was JSON. It looks like this: {“BusName”:”Joe”,”BusPhone”:”1234567890”} what I want to do is split it into two variables, (buiessname = BusName and businessphone = BusPhone), and also remove all the {}, ” and :’s.

I could use split and replace but it would be a messy function, is there some kind of Java/JSON function that can handle it for me??? How would you guys go about it???

Cheers,

Mike.

2
  • The procedure you are describing is known as "JSON Parsing", here is full example for the same: pareshnmayani.wordpress.com/category/android/json-parsing Commented Nov 6, 2011 at 7:52
  • Correct, except that your example uses the JSON implementation provided by Android. While you can do that if you really want to, there are alternative API's that are much more convenient to work with, and simple to include in your project. Commented Nov 6, 2011 at 8:03

2 Answers 2

6

You can use JSONObject to parse the JSON String into a real object.

String jsonStr = "{\“BusName\”:\”Joe\”,\”BusPhone\”:\”1234567890\”}";
JSONObject myJsonObj = new JSONObject(jsonStr);

String busName = myJsonObj.getString("BusName");
String busPhone = myJSONObj.getString("BusPhone");
Sign up to request clarification or add additional context in comments.

2 Comments

I don't, it's not a complete example. If you're going to use the JSON support that is built-in to Android (which I assume is what this answer is suggesting), then you need to have all your JSON-related calls in a try/catch block because every single operation can stupidly throw a JSONException. The code example shown here omits the try/catch, and is incomplete.
I'm aware of the try/catch and I assume @Mike has already knew how to handle the exception. I just show him that the function to parse JSON string into object indeed exist. The question is not "how to handle the JSON exception while parsing" but "is there some kind of Java/JSON function that can handle it". It's crystal clear.
2

I'd suggest using json-simple to parse the JSON data, rather than trying to directly manipulate the string yourself. For example, you might do:

JSONObject data = (JSONObject)JSONValue.parse(text);
String businessName = (String)data.get("BusName");
String businessPhone = (String)data.get("BusPhone");

You can find more examples here: http://code.google.com/p/json-simple/wiki/DecodingExamples.

5 Comments

Yes, the get() function exists. The JSONObject implementation provided by json-simple extends HashMap, and supports all the standard Map operations. All the more reason to prefer it over other JSON libraries. You can check the source if you want: code.google.com/p/json-simple/source/browse/trunk/src/org/json/…
BTW, i also prefer native classes instead of using 3rd party libraries like GSON, just check the example link posted as comment :)
The problem with the "native" classes is that Google decided to bundle the json.org implementation that throws JSONException all over the place. It's a complete pain to use. Other, better, more developer-friendly libraries exist and are simple to add to your project. There's very little reason to prefer the version bundled with Android.
yes pain is of JSONException but we dont have to look outside
Thanks Kristiono, I was hoping there was a function to do it, I almost had the split find function going in the mean time. I’m a .NET developer just happened that way but I’m loving Java, I did a bit of it back in Uni and it reminds me of good old C! Also Aroth, I added the try/catch block, Eclipse wouldn’t let me not add it. Cheers, Mike.

Your Answer

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