Android Open Source - openmbta-android Json Parser






From Project

Back to project page openmbta-android.

License

The source code is released under:

Copyright (c) 2014 Kaja Software Corporation Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal...

If you think the Android project openmbta-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kaja.openmbta;
//from w w  w. j  a  v a 2 s .  c  o  m
import org.json.JSONArray;
import org.json.JSONObject;

 
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import android.util.Log;

public class JsonParser extends Activity {
  
  private TextView tv;
  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()\"}]}}}";
    private String result;
  
  @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 {
    
    setContentView(R.layout.textview);
        tv = (TextView)findViewById(R.id.myTV);

    jObject = new JSONObject(jString);

    JSONObject menuObject = jObject.getJSONObject("menu");
    String attributeId = menuObject.getString("id");
    System.out.println(attributeId);
    result = "AttrId: " + attributeId + "\n";
    //tv.setText("AttrId:" + attributeId);
    
    String attributeValue = menuObject.getString("value");
    System.out.println(attributeValue);
    result = result +  "AttrVal:" + attributeValue + "\n";

    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());
      result = result +  "value: " + menuitemArray.getJSONObject(i).getString("value").toString() + "\n";
      System.out.println(menuitemArray.getJSONObject(i).getString(
          "onclick").toString());
      result = result +  "Onclick: " + menuitemArray.getJSONObject(i).getString("onclick").toString() + "\n";    }
    
    tv.setText(result);
  }
}




Java Source Code List

com.kaja.openmbta.JsonParser.java
com.kaja.openmbta.KajaItemizedOverlay.java
com.kaja.openmbta.MergeAdapter.java
com.kaja.openmbta.OpenMBTA.java
com.kaja.openmbta.RItemizedOverlay.java
com.kaja.openmbta.RoutesItemizedOverlay.java
com.kaja.openmbta.Routes.java
com.kaja.openmbta.ScheduleAdapter.java
com.kaja.openmbta.ScheduleItem.java
com.kaja.openmbta.ScheduleTab.java
com.kaja.openmbta.SplashScreen.java
com.kaja.openmbta.TransDetails.java
com.kaja.openmbta.WebViewer.java
com.kaja.openmbta.hashFile.java
com.kaja.openmbta.transAdapter.java
com.kaja.openmbta.transItem.java
com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay.java
com.readystatesoftware.mapviewballoons.BalloonOverlayView.java