REST and JSON - convert a string to a JSON array

I am new to JSON and REST. I work with a server that returns lines like this:

[{"username":"Hello","email":"hello@email.com","credits":"100","twitter_username":""},{"username":"Goodbye","email":"goodbye@email.com","credits":"0","twitter_username":""}]

I managed to print them as strings on the console, but now I want to convert them to a JSON array. The code that I still do not return any errors, but I do not know what to add to the constructor for the new JSON array. I meant the part of the code sent to me by a colleague in which the constructor was the new JSONArray (answer), but he never told me what the "answer" is.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import sun.misc.BASE64Encoder;

public class NetClientGet {

    public static void main(String[] args) {

      try {

        URL url = new URL("http://username:password@mobile.crowdedmedia.co.uk/index.php/api/users/get_users/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        BASE64Encoder enc = new sun.misc.BASE64Encoder();
        String userpassword = "username:password";
        String encoded = enc.encode(userpassword.getBytes());
        conn.setRequestProperty("Authorization", "Basic " + encoded);

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        JSONArray array = new JSONArray(output);

        for (int i =0; i < array.size(); i++) {
            JSONObject row = array.getJSONObject(i);
            String user = row.getString("username");
            System.out.println(user);
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

      }

    }
}
+5
source share
3 answers
  • use GSON to format a string like JsonArray
  • then go to JsonArray to get the values

code example

String json = "[{\"username\":\"Hello\",\"email\":\"hello@email.com\",\"credits\":\"100\",\"twitter_username\":\"\"},{\"username\":\"Goodbye\",\"email\":\"goodbye@email.com\",\"credits\":\"0\",\"twitter_username\":\"\"}]";
JsonArray jArray = new JsonParser().parse(json).getAsJsonArray();
for (int i=0;i<jArray.size();i++) {
    JsonObject jsonObject = jArray.get(i).getAsJsonObject();
    System.out.println(jsonObject.get("username"));
    System.out.println(jsonObject.get("email"));
    System.out.println(jsonObject.get("credits"));
    System.out.println(jsonObject.get("twitter_username"));
    System.out.println("*********");
}
+11
source

gson json. gson . json. json-, json:

JsonParser parser = new JsonParser();

json

JsonArray jArray = new JsonArray();

json

jArray = parser.parse(outputString).getAsJsonArray();
+1

org.json.simple.JSONObject org.json.simple.JSONArray, , net.sf.json.JSONArray .

( JSONArray = JSONArray ();)

 {"YOUR_ARRAY_NAME": [{"username":"Hello","email":"hello@email.com","credits":"100","twitter_username":""},{"username":"Goodbye","email":"goodbye@email.com","credits":"0","twitter_username":""}]}

JSONArray.

0

All Articles