PowenKo, Android , Data, org.json.JSONArray

May 19, 2012   ·   0 Comments

package com.powenko.Tutorial_json;

import android.app.Activity;
import android.os.Bundle;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


public class Tutorial_jsonActivity extends Activity {
    /** Called when the activity is first created. */
 
    	private JSONObject jObject;
 private String jString = 
	 "{\"menu\":" +
	         " {\"id\": \"file\"," +
	          " \"value\": \"File\", " +
	          "\"popup\": { \"menuitem\": " +
 		                  "[ {\"value\": \"New\", " +
 		                    "   \"onclick\": \"CreateNewDoc()\"}, " +
 		                    "{\"value\": \"Open\"," +
 		                    " \"onclick\": \"OpenDoc()\"" +
 		                    "}," +
 		                    " {\"value\": \"Close\"," +
 		                      " \"onclick\": \"CloseDoc()\"}]}}}";
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		try {	parse();
    		} catch (Exception e) {
    			e.printStackTrace();	}
    	}
    	private void parse() throws Exception {
    		jObject = new JSONObject(jString);

    		JSONObject menuObject = jObject.getJSONObject("menu");
    		String attributeId = menuObject.getString("id");
    		System.out.println(attributeId);

    		String attributeValue = menuObject.getString("value");
    		System.out.println(attributeValue);

    		JSONObject popupObject = menuObject.getJSONObject("popup");
    		JSONArray menuitemArray = popupObject.getJSONArray("menuitem");

    		for (int i = 0; i < 3; i++) {
    			System.out.println(menuitemArray.getJSONObject(i)
    					.getString("value").toString());
    			System.out.println(menuitemArray.getJSONObject(i).getString(
    					"onclick").toString());
    			//Log.v("Print","Hi");
    			Log.d("Print","Hi");
    		}
    	}

    		  
    
}
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon