0

I am trying to parse URL to get JSON response and particular values from that response. I dont have a sample code. Please give me a simple solution. Below I have posted my URL and Response. I want to GET "School", Name" and Result values.

http://sample.com/login/username/ <username> /password <password>?  

{
    "response":{
                "School":"SBOA",
                "Name":"Anitha",
                "Class":"Tenth",
              },
                "Result":"Good",
}

My Code :

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView output = (TextView) findViewById(R.id.textView1);
        String strJson="URL";
        String data = "";
        System.out.println(strJson);

        try {
            JSONObject  jsonRootObject = new JSONObject(strJson);

            //Get the instance of JSONArray that contains JSONObjects
            JSONArray jsonArray = jsonRootObject.optJSONArray("response");
            System.out.println(jsonRootObject);

            //Iterate the jsonArray and print the info of JSONObjects
            for(int i=0; i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);

            //    int id = Integer.parseInt(jsonObject.optString("id").toString());
                String name = jsonObject.optString("School").toString();
            //   float salary = Float.parseFloat(jsonObject.optString("salary").toString());

             //   data += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Salary= "+ salary +" \n ";*/
            }
            //output.setText(data);
        } catch (JSONException e) {e.printStackTrace();}
    }
}
android
  • 453
  • 3
  • 7
  • 14
  • 3
    We are not here for doing your home work. do by your self or search on _Google_ `how to parse JSON in Android`? – M D Oct 19 '15 at 12:57
  • What you have tried ? – Jaiprakash Soni Oct 19 '15 at 12:57
  • I have tried by using lots of sample code but. Nothing simple soltion. I am doing login activity. If I parse that URL, then once the URL got response, I need to go another activity. – android Oct 19 '15 at 13:01
  • 1
    Possible duplicate of [Sending and Parsing JSON in Android](http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android) – Rahul Tiwari Oct 19 '15 at 13:05

2 Answers2

0

If you have a JSONObject named json then follow this for getting school value

try{
jsonRootObject .getJSONObject("response").getString("School");
}catch(JSONException e)
{
e.printStackTrace();
}
Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42
  • Thank you @Warlock. I have posted my code Could you please look at. Its a good way of parse? – android Oct 19 '15 at 13:11
  • What are you trying to do actually, because your code not shows , how you get the JSON – Lakhwinder Singh Oct 19 '15 at 13:13
  • I need to Parse Above I have mentioned sample URL with multiple (User name and Password)Parameters. Then if the URL gave correct response I need to go home activity. Its a Login process. – android Oct 19 '15 at 13:14
0

To read exactly that JSON, use this:

/** Verify that your strJson string contains this:
 * {
 *   "response":{
 *               "School":"SBOA",
 *               "Name":"Anitha",
 *               "Class":"Tenth",
 *             },
 *               "Result":"Good",
 * }
 */
String strJson = ??;
Log.d("TAG", "strJson: " + strJson);
try {
    JSONObject jsonRootObject = new JSONObject(strJson);
    JSONObject response = jsonRootObject.getJsonObject("response");
    String schoolString = response.getString("School");
    String nameString = response.getString("Name");
    String classString = response.getString("Class");

    String result = jsonRootObject.getString("Result");
} catch(JSONException e) {
    Log.e("TAG", "Error reading json: " + jsonRootObject.toString());
}
Joakim
  • 3,224
  • 3
  • 29
  • 53
  • Great Help! Thank you Buddy! – android Oct 19 '15 at 14:49
  • I am facing this error buddy : org.json.JSONException: Value http of type java.lang.String cannot be converted to JSONObject..@Joakim – android Oct 19 '15 at 14:54
  • verify that the strJson String contains valid JSON. Your error indicates that it contains the string "http". Verify that you are actually downloading some JSON data and putting it in your strJson object – Joakim Oct 19 '15 at 14:56
  • If you are unsure whether your String contains valid JSON, use for example http://jsonlint.com/ and paste the content there. My code snippet will only work if JSONLint says your text is valid Json. – Joakim Oct 19 '15 at 14:57
  • I am passing JSON URL Buddy! Like "http://.....com/login/username/ /password ?" – android Oct 19 '15 at 14:58
  • You have to download the JSON data from that Url before you parse it. The link itself doesn't contain the school, name, class information... – Joakim Oct 19 '15 at 14:59
  • No. If I parse Username and password with my URL, Then JSON response will give school etc informations! Please help me buddy! – android Oct 19 '15 at 15:01
  • change the getString(xx) to start with the capital letter. response.getString("School"); instead of response.getString("school"); – Joakim Oct 19 '15 at 15:05
  • Now nothing error. but its not comming into try. Nothing values printed buddy!Please help me! – android Oct 19 '15 at 15:16