18 April, 2013

JSONArray and JSONObject Example


Data just look like:


{
       "Id": "10MCA001",
       "Bdate": "30/07/1990",

       "Score": 
       [
           {
               "mid_Exam": 25,
               "outOf": 30
           },
           {
               "gtu_Exam": 231,
               "outOf": 1570
           }
       ],
       "Report": 
       [
           {
               "college_Remarks": "Good",
               "sports_Remarks": "Best",
               "type_of_Sport" : "Cricket"
           }
       ]
   }


MainActivity.java


package com.example.json_ums_test;

import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity 
{

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

TextView JsonSrc = (TextView)findViewById(R.id.json_src);
   TextView JsonResult = (TextView)findViewById(R.id.json_result);

   
//    {
//        "Id": "10MCA001",
//        "Bdate": "30/07/1990",
//
//        "Score": 
//        [
//            {
//                "mid_Exam": 25,
//                "outOf": 30
//            },
//            {
//                "gtu_Exam": 231,
//                "outOf": 1570
//            }
//        ],
//        "Report": 
//        [
//            {
//                "college_Remarks": "Good",
//                "sports_Remarks": "Best",
//                "type_of_Sport" : "Cricket"
//            }
//        ]
//    }

   final String customJSON = " {\"Id\":\"10MCA001\",\"Bdate\":\"30/07/1990\",\"Score\":[{\"mid_Exam\":25,\"outOf\":30},{\"gtu_Exam\":231,\"outOf\":1570}],\"Report\":[{\"college_Remarks\":\"Good\",\"sports_Remarks\":\"Best\",\"type_of_Sport\":\"Cricket\"}]} ";
   JsonSrc.setText(customJSON);
   
   try
   {
    JSONObject jsonObject = new JSONObject(customJSON);
   
    String myId = jsonObject.getString("Id");
    String myBdate = jsonObject.getString("Bdate");

   
    String stringJsonResult = "Id: " + myId + "\n"
            + "Bdate: " + myBdate;
   
   
   
    JSONArray jsonArray = jsonObject.getJSONArray("Score");
    JSONObject arrayElement_Score = jsonArray.getJSONObject(0);
    String mid_score = arrayElement_Score.getString("mid_Exam");
    String outof = arrayElement_Score.getString("outOf");
       
    stringJsonResult += "\n\nScore:"
    + "\n\n:: Mid-Semester Exam ::\n"
            + "Score: " + mid_score + "\t"
            + "Out Of: " + outof;
   
    JSONObject arrayElement_Score1 = jsonArray.getJSONObject(1);
    String gtu_score = arrayElement_Score1.getString("gtu_Exam");
    String outof1 = arrayElement_Score1.getString("outOf");
       
    stringJsonResult += 
    "\n\n:: GTU Exam ::\n"
            + "Score: " + gtu_score + "\t"
            + "Out Of: " + outof1;
   
   
   
    JSONArray jsonArray1 = jsonObject.getJSONArray("Report");
    JSONObject arrayElement_Report = jsonArray1.getJSONObject(0);
    String college_Remarks = arrayElement_Report.getString("college_Remarks");
    String sports_Remarks = arrayElement_Report.getString("sports_Remarks");
    String type_of_Sport = arrayElement_Report.getString("type_of_Sport");
   
    stringJsonResult += "\n\nReport: \n"
            + "College Remarks: " + college_Remarks + "\n"
            + "Sports Remarks: " + sports_Remarks+ "\n"
            + "Sport Type: " + type_of_Sport;
   
   
    JsonResult.setText("\n" + stringJsonResult);
   
   }
   catch (JSONException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
   }
   
}
}

activity_main.xml



<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/json_src"
        android:layout_width="280dp"
        android:layout_height="106dp"
        android:layout_x="26dp"
        android:layout_y="23dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/json_result"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_x="25dp"
        android:layout_y="134dp"
        android:text="TextView" />

</AbsoluteLayout>


ScreenShot


JSONArray and JSONObject Example
JSONArray and JSONObject Example




No comments:

Post a Comment