9 – Introduction to JSON- Part 2

In this video

Follow us on Social Media

You might be interested in

Comment (7)

  1. Sir JSON file is not working, please review below mention code.

    package com.example.muhammad.myapplication;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.io.InputStream;

    public class Json extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_json);

    try {
    JSONObject obj=new JSONObject(JsonLoad());
    JSONArray marray=obj.getJSONArray(“q1”);

    for (int i=0; i<marray.length(); i++)
    {
    JSONObject jinside=marray.getJSONObject(i);
    String question=jinside.getString("question");
    String options=jinside.getString("options");
    Toast.makeText(getApplicationContext(),""+question,Toast.LENGTH_SHORT).show();
    Toast.makeText(getApplicationContext(),""+options,Toast.LENGTH_SHORT).show();
    }

    } catch (JSONException e) {
    e.printStackTrace();
    }
    }

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

    JSON File
    ======================

    {
    "quiz": {
    "sport": {
    "q1": {
    "question": "Which one is correct team name in NBA?",
    "options": [
    "New York Bulls",
    "Los Angeles Kings",
    "Golden State Warriros",
    "Huston Rocket"
    ],
    "answer": "Huston Rocket"
    }
    },
    "maths": {
    "q1": {
    "question": "5 + 7 = ?",
    "options": [
    "10",
    "11",
    "12",
    "13"
    ],
    "answer": "12"
    },
    "q2": {
    "question": "12 – 8 = ?",
    "options": [
    "1",
    "2",
    "3",
    "4"
    ],
    "answer": "4"
    }
    }
    }
    }

LEAVE YOUR COMMENT

Your email address will not be published. Required fields are marked *