0

So I have a Json file that I have parsed, and I want to know how I can get the information in the Json file to the screen? Heres the parsing of the Json.

public String loadJSONFromAsset()
{
    String json = null;
    try
    {
        InputStream is = getAssets().open("JSON.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);
        is.close();

        json = new String(buffer, "UTF-8");
    }
    catch(IOException ex)
    {
        ex.printStackTrace();
        return null;
    }
    return json;
}

JSON:

{"answers":[
{
"answer": "1"
},
{
"answer": "2"
},
{
"answer": "3"
},
{
"answer": "4"
},
{
"answer": "5"
}
]}

How do I get for example "4" into a variable? I hope my question is clear enough, if not, comment. What I want to do is simply output one of the numbers.

random1234
  • 777
  • 3
  • 17
  • 41

4 Answers4

1

Parse the JSON String.

JSONObject obj = new JSONObject(jsonString);
JSONArray answers = obj.getJSONArray("answers");
for (int i = 0; i < answers.length(); i++) {
    // this will loop though all answers
    JSONObject answerObj = answers.getJSONObject(i);
    String answer = answerObj.getString("answer");        
}

I suggest searching for some examples on how to parse JSON in java, there are plenty of examples and tutorials

Denny
  • 1,766
  • 3
  • 17
  • 37
1

Use this sample code hope it will solve your problem

 private void parseJson(String json) {
    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray jsonArray = jsonObject.getJSONArray("answers");
        for (int i = 0; i < jsonArray.length(); i++){
            String answer = jsonArray.getJSONObject(i).getString("answer");
            Log.i(TAG,"answer: "+answer);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Hasnain
  • 310
  • 2
  • 10
  • Sorry for the possibly dumb question but do I not have to call this method? If so, do I call it from oncreate or from the "loadJSONFromAsset" method? Currently I'm calling the "loadJSONFromAsset" method from the oncreate method, should I do the same with this one, or call it from within the first method? – random1234 May 15 '17 at 12:26
  • 1
    call loadJSONFromAsset method assign output in parseJson and done. its not long running task no need to create separate thread for this make call link parseJson(loadJSONFromAsset ()); – Hasnain May 15 '17 at 12:35
  • @Hasnain Thanks man. However is there anything I'm doing wrong in "opening" my Json file? the getAssets().open("JSON.json"); line. Is there anything wrong here? The Json file is in the "assets" folder of the project, am I referencing it correctly? Because i think i'm getting some errors with that. – random1234 May 15 '17 at 12:52
  • what error you are getting here getAssets().open("JSON.json"); line – Hasnain May 15 '17 at 13:13
0

You'll need one of Java Json libraries for that.

    final JSONObject j = new JSONObject(parsed);

    JSONArray answers = j.getJSONArray("answers");

    System.out.println(answers.get(4));
Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
0
You should used this sample code

 public String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getAssets().open("yourfilename.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}

Read Json File Like This always used try and catch block

try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONArray m_jArry = obj.getJSONArray("answers");

        for (int i = 0; i < m_jArry.length(); i++) {

            JSONObject jo_inside = m_jArry.getJSONObject(i);
            String answer = jo_inside.getString("answer");

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31