Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How can used Static JSON Parsing in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 166
    Comment on it

     In the below example I have created  Static JSON Parsing program in android. First I have added two TextView within RelativeLayout. In MainActivity I have used JSON object and JSON array JSON object within curly bracket ({)  and JSON array  store in square bracket ([). JSON object contains  key-value pairs. In below example Key are id, blog_name and publish_date. Key-Value pairs are separated by colon ” : “. JSON array is a set of objects. here in below code JSON array key name is "Blogs”. You can see below program it will clearly describe to used Static JSON Parsing in Android.

    Step(1)-main_activity.xml layout-

      <RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Static JSON Parsing c "
            android:textStyle="bold"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textSize="38dp"
            android:textColor="#230cff" />;
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:textSize="25dp"
            android:textStyle="bold"
            android:id="@+id/textView1"
            android:layout_marginTop="42dp"
            android:layout_below="@+id/textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textColor="#000000" />;
    
        </RelativeLayout>

    Step(2)-MainActivity-

     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 = "{\"Blogs\":[{\"id\": \"01\",\"blog_name\": \"How can used java key value?\",\"publish_date\": \"12 jan 2014\"},{\"id\": \"02\",\"blog_name\": \"How to create JSON object?\",\"publish_date\": \"4 sept 2015\"},{\"id\": \"03\",\"blog_name\": \"How to Generate google map key\",\"publish_date\": \"4 dec 2015\"}]}";
                String data = "";
                try {
                    JSONObject jsonRootObject;
                    jsonRootObject = new JSONObject(strJson);
    
                    //Get the instance of JSONArray that contains JSONObjects
                    JSONArray jsonArray = jsonRootObject.optJSONArray("Blogs");
    
                    //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 blog_name = jsonObject.optString("blog_name").toString();
                        String publish_date = jsonObject.optString("publish_date").toString();
                        data += "Blog Number "+(i+1)+" : \n id= "+ id +" \n Blog Name= "+ blog_name +" \n Publish Date= "+ publish_date +" \n\n\n\n ";
                    }
                    output.setText(data);
                } catch (JSONException e) {e.printStackTrace();}
            }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: