1

I'm relatively new to using JSON and all I really need to do read in a few key value pairs from a JSON file on the file system. What I figured I would do is read in the file as a string and then parse it that way but it seems kind of redundant that way.

Here's what my file will be like:

 {
 "username" : "myname"
 "domain" : "mydomain"
 }

So essentially I need some help making an easy and efficient block of code to read in the key/value pairs. I've been trying to use GSON for the most part and haven't had much luck with examples I've found.

Thanks everyone

The Whether Man
  • 362
  • 5
  • 21
  • Discussion on question [Converting JSON to Java](http://stackoverflow.com/q/1688099/214178) mentions some points about GSon and some alternatives. – artdanil Jul 24 '13 at 21:57

1 Answers1

2

One other alternative is JSON.org, in which creating a JSON object from a JSON string requires only one line:

JSONObject jsonObject = new JSONObject(someJSONString);

When you need to access its value, use the functions that the JSONObject provides. For example,

String userName = jsonObject.getString("username");
String domainName = jsonObject.getString("mydomain");
keelar
  • 5,814
  • 7
  • 40
  • 79