简体   繁体   中英

Java - Need help regarding JSON array/object and validation

I have a task for school where I have to authenticate to a server. Once authenticated I can collect(not sure if it's the right word) some JSON data, using:

List <NameValuePair> nvp = new ArrayList <NameValuePair>();

nvp.add(new BasicNameValuePair("username", "test")); //used to log in to the site
nvp.add(new BasicNameValuePair("password", "test")); 
nvp.add(new BasicNameValuePair("request", "login"));
nvp.add(new BasicNameValuePair("request", "mctrainer"));
post.setEntity(new UrlEncodedFormEntity(nvp));

The data I collect using that code is:

[{"correctanswerPK":"155","challenge":"Hva er Areal","answer":"En klasse","choices":["Heltallstype","En klasse","Konstruktør"]},{"correctanswerPK":"149","challenge":"Hva brukes ordet private til","answer":"Innkapsling","choices":["Innkapsling","En klasse","Heltallstype"]},{"correctanswerPK":"161","challenge":"Hva er double","answer":"Flyttallstype","choices":["Exception","Flyttallstype","En metode"]}]

It's Norwegian data, but it's JSON nevertheless.

I'm currently 'displaying' this data by running a System.out.println() , which means it appears in the console. However, the task is to make it appear in a JFrame. My understanding is that I need to use JSONArray and JSONObject to be able to do this, plus a JSONvalidator or something similar.

The problem is that I have no idea how to do that. Any chance someone could help me out?

My teacher told us jtwitter.jar v2.6.9 was a good library for doing this, but I'm not sure how to use it. Any other ideas?

I presume you want to split up the JSON text into individual objects to display in your UI (since you could just display the text, but I guess that's not the point of your assignment?).

I wouldn't recommend using a library that wasn't built with the central goal for which you need it. Not to cast doubt on your professor, but the library he recommended to you was designed to retrieve Twitter data, not to parse JSON (which it may well do as a side effect of its primary goal, but thats still not its main focus). I would recommend any of the other excellent JSON libraries all of which are capable of both JSON validation and conversion to POJOs (plain old java objects). Here is a decent list to get you started

Java JSON is dead simple to integrate into your project and use - it's literally less than 10 classes you download and include directly with your code. Gson is probably the next easiest to use, followed by Jackson which is the heavyweight of the three, but also the most powerful. Below I show an example processing using the easiest library on the list. It assumes JSON of the form:

[
    {"firstName" : "Bob", "lastName" : "Jones"},
    {"firstName" : "Susan", "lastName" : "Smith"},
    {"firstName" : "Melody", "lastName" : "Dumond"}
]

Which is a JSON array similar to your data.

final JSONArray jsonArr = new JSONArray(json);
final int numObjs = jsonArr.length();
for(int i = 0 ; i < numObjs; i++) {
    final JSONObject jsonObj = jsonArr.getJSONObject(i);
    System.out.printf("Person's name is %s, %s\n", jsonObj.get("lastName"),
        jsonObj.get("firstName");
}

You can extend off this example for your own project, or study and incorporate one of the other useful libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM