-2

I know this question has been ask many time, but I can't find a good solution when I parse the json in java. for example the server return a string {"Key":"9c4c"} how can I parse this kind of String, although this may not be the standard Json. Can somebody help? thanks

mt73564
  • 11
  • 1
  • What have you tried? I will post some code in a second – Sander bakker Jun 30 '17 at 09:47
  • You know it has been asked many times, so presumably you have found solutions. Why were the solutions you found not good enough? There's nothing non-standard about that little bit of JSON that you posted. – Jesper Jun 30 '17 at 10:03
  • I have read other post, but the json in these post are different. I am new to json, and my json format is {"key":"abc123"}, for example, in this format. and I don't know which jar to import too. – mt73564 Jul 03 '17 at 02:11

1 Answers1

0

This is a way to do it, code is commented for explanation:

public class JSONParser {
    String url;
    String requestMethod;
    public JSONParser(String url, String requestMethod){
        this.url = url;
        this.requestMethod = requestMethod;
    }
    private String read(BufferedReader bufferedReader) throws IOException {
        //Creates new StringBuilder to avoid escaping chars

        StringBuilder stringBuilder = new StringBuilder();
        //Creates new variable

        String currentLine;
        //Gets the currentLine

        while((currentLine = bufferedReader.readLine()) !=null ){

            //Adds the currentLine to the stringBuild if the currentLine is not null

            stringBuilder.append(currentLine);
        }
        //Returns the StringBuilder is String format

        return stringBuilder.toString();
    }

    public JSONObject sendRequest() throws IOException, JSONException {
        //Get the URL from the constructor

        URL url = new URL(this.url);
        //Opens the connection

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //Sets the requestMethod

        connection.setRequestMethod(this.requestMethod);
        //Sets the requestProperties

        connection.setRequestProperty("Content-type", "application/JSON");
        //Tries to read the through the BufferedReader

        try {
            //Creates new BufferedReader & InputStreamReader which get the input of the connection

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            //Stores the output of the connection in a string

            String jsonText = read(bufferedReader);
            //Creates a new JSON object from the above String

            JSONObject json = new JSONObject(jsonText);
            //Returns the JSON Object

            return json;
        } finally {
            //Disconnects from the URL
            connection.disconnect();
        }
    }

    public void storeJSONData(){
        try{
            //Sends request
            JSONObject json = sendRequest();
            //Gets the JSON array, after that the first JSON object
            json.getJSONArray("").getJSONObject(0).getString("Key");
        }
        catch (IOException e){
            System.out.print(e);
        }
        catch (JSONException e){
            System.out.print(e);
        }


    }
}
Sander bakker
  • 518
  • 1
  • 6
  • 20
  • Many thanks to your kindly answer and help, Sander. But I have an problem when I follow your code: org.json.JSONException: JSONObject[""] not found. May I ask how will you print the key value and which .jar do you use? – mt73564 Jul 03 '17 at 03:01