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

1
  • Discussion on question Converting JSON to Java mentions some points about GSon and some alternatives. Commented Jul 24, 2013 at 21:57

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

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.