Android Open Source - markj Jw J S O N Reader






From Project

Back to project page markj.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project markj 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.markjmind.mobile.api.android.json;
//  w  w w.jav a2 s.c  o  m
import java.util.ArrayList;
import java.util.Iterator;

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

/**
 * start : 2013.11.17<br>
 * <br>
 * 
 * @author ???
 * @version 2013.11.17
 */
public abstract class JwJSONReader<ParentResult> {
  boolean isElementSort = true;
  public static enum JSONType{
    Object,
    JSONObject,
    JSONArray
  }
  public void setElementSort(boolean isElementSort){
    this.isElementSort = isElementSort;
  }
  public void read(JSONObject json) throws JSONException{
    read(json,null,null);
  }
  
  public void read(JSONObject json, String rootName) throws JSONException{
    read(json,null,rootName);
  }
  
  private void read(JSONObject json, ParentResult parentResult, String parentName) throws JSONException{
    Iterator iter = json.keys();
    ArrayList<ElementArraySort> jsonArrayList = new ArrayList<ElementArraySort>();
    ArrayList<ElementObjectSort> jsonObjectList = new ArrayList<ElementObjectSort>();
    JSONArray jsonArray;
    String name;
    while(iter.hasNext()){
      name = (String)iter.next();
      JSONType type = getJSONType(json, name);
      switch (type) {
      case Object:
        work(json, name, parentName, parentResult);
        break;
      case JSONObject:
        if(isElementSort){
          jsonObjectList.add(new ElementObjectSort(name, json.optJSONObject(name)));
        }else{
          recallWork(json.optJSONObject(name), name,parentResult,startArrayWork(name,parentResult));
        }
        
        break;
      case JSONArray:
        if(isElementSort){
          jsonArrayList.add(new ElementArraySort(name,getJSONArray(json, name)));
        }else{
          jsonArray =getJSONArray(json, name);
          for(int i=0;i<jsonArray.length();i++){
            recallWork(jsonArray.getJSONObject(i), name, parentResult, startArrayWork(name, parentResult));
          }
        }
        break;
      }
    }
    if(isElementSort){
      ElementObjectSort eos;
      for(int i=0;i<jsonObjectList.size();i++){
        eos =  jsonObjectList.get(i);
        recallWork(eos.json, eos.name, parentResult,startArrayWork(eos.name,parentResult));
      }
      ElementArraySort es;
      for(int j=0;j<jsonArrayList.size();j++){
        es =  jsonArrayList.get(j);
        for(int i=0;i<es.jsonArray.length();i++){
          recallWork(es.jsonArray.getJSONObject(i), es.name, parentResult, startArrayWork(es.name, parentResult));
        }
      }
    }
  }
  
  private class ElementObjectSort{
    public String name;
    public JSONObject json;
    public ElementObjectSort(String name, JSONObject json){
      this.name = name;
      this.json =json;
    }
  }
  private class ElementArraySort{
    public String name;
    public JSONArray jsonArray;
    public ElementArraySort(String name, JSONArray jsonArray){
      this.name = name;
      this.jsonArray =jsonArray;
    }
  }
  private void recallWork(JSONObject sub_json, String name, ParentResult parentResult,ParentResult result) throws JSONException{
    if(sub_json!=null){
      read(sub_json, result,name);
    }
    endArrayWork(name,parentResult);
  }
  
  public abstract void work(JSONObject json, String name,String parentName, ParentResult parentResult);
  public abstract  ParentResult startArrayWork(String name, ParentResult parentResult);
  public abstract void endArrayWork(String name, ParentResult parentResult);
  
  public static JSONType getJSONType(JSONObject json, String name) throws JSONException{
    JSONType type=null;
    Object object = json.get(name);
    if(object instanceof JSONObject){
      type = type.JSONObject;
    }else if(object instanceof JSONArray){
      type = type.JSONArray;
    }else{
      type = type.Object;
    }
    return type;
  }
  
  public static JSONArray getJSONArray(JSONObject json, String name) throws JSONException{
    JSONArray array =null;
    if(!json.isNull(name)){
      JSONType type = getJSONType(json,name);
      switch (type) {
      case Object:
        array = new JSONArray();
        array.put(json.get(name));
        break;
      case JSONObject:
        JSONObject gateway = json.getJSONObject(name);
        array = new JSONArray();
        array.put(gateway);
        break;
      case JSONArray:
        array = json.getJSONArray(name);
      }
    }
    return array;
  }
}




Java Source Code List

com.markjmind.mobile.api.android.controller.JwAsyncTask.java
com.markjmind.mobile.api.android.controller.JwMapperException.java
com.markjmind.mobile.api.android.controller.JwMemberMapper.java
com.markjmind.mobile.api.android.controller.JwStringID.java
com.markjmind.mobile.api.android.controller.JwViewController.java
com.markjmind.mobile.api.android.controller.JwViewerCache.java
com.markjmind.mobile.api.android.controller.JwViewer.java
com.markjmind.mobile.api.android.controller.Jwc.java
com.markjmind.mobile.api.android.controller.OnClickListenerReceiver.java
com.markjmind.mobile.api.android.controller.ViewerXmlMapper.java
com.markjmind.mobile.api.android.exception.JwErrorCode.java
com.markjmind.mobile.api.android.exception.JwException.java
com.markjmind.mobile.api.android.json.JSONMsgString.java
com.markjmind.mobile.api.android.json.JwJOSNTreeArray.java
com.markjmind.mobile.api.android.json.JwJSONReader.java
com.markjmind.mobile.api.android.ui.JwAnimation.java
com.markjmind.mobile.api.android.ui.JwGroup.java
com.markjmind.mobile.api.android.ui.JwMenu.java
com.markjmind.mobile.api.android.ui.JwOnGroupSelect.java
com.markjmind.mobile.api.android.ui.JwTabGroup.java
com.markjmind.mobile.api.android.ui.JwTouchGroupListener.java
com.markjmind.mobile.api.android.ui.JwTouchGroup.java
com.markjmind.mobile.api.android.ui.JwTouchMotionGroupLisener.java
com.markjmind.mobile.api.android.ui.JwTouchMotionGroup.java
com.markjmind.mobile.api.android.ui.drawgraph.GraphBoard.java
com.markjmind.mobile.api.android.ui.drawgraph.GraphSkin.java
com.markjmind.mobile.api.android.ui.drawgraph.HBackLineSkin.java
com.markjmind.mobile.api.android.ui.drawgraph.LineGraphSkin.java
com.markjmind.mobile.api.android.ui.drawgraph.PieGraph.java
com.markjmind.mobile.api.android.ui.drawgraph.RectGraphSkin.java
com.markjmind.mobile.api.android.ui.drawgraph.SimpleStyle.java
com.markjmind.mobile.api.android.ui.drawgraph.TextArraySkin.java
com.markjmind.mobile.api.android.ui.drawgraph.VBackLineSkin.java
com.markjmind.mobile.api.android.ui.graph.Arc.java
com.markjmind.mobile.api.android.ui.graph.BarElement.java
com.markjmind.mobile.api.android.ui.graph.BarLayout.java
com.markjmind.mobile.api.android.ui.graph.Bar.java
com.markjmind.mobile.api.android.ui.graph.IPieSkin.java
com.markjmind.mobile.api.android.ui.graph.LineLayout.java
com.markjmind.mobile.api.android.ui.graph.MeasureLayout.java
com.markjmind.mobile.api.android.ui.graph.PieArcRoundSkin.java
com.markjmind.mobile.api.android.ui.graph.PieLayout.java
com.markjmind.mobile.api.android.ui.graph.PiePercentSkin.java
com.markjmind.mobile.api.android.ui.view.JwBaseDropDown.java
com.markjmind.mobile.api.android.ui.view.JwDropDown.java
com.markjmind.mobile.api.android.ui.view.JwSlidingLayout.java
com.markjmind.mobile.api.android.ui.view.JwWheelView.java
com.markjmind.mobile.api.android.ui.view.StickerListener.java
com.markjmind.mobile.api.android.ui.view.StickerView.java
com.markjmind.mobile.api.android.ui.view.textview.JwTextViewAutoScale.java
com.markjmind.mobile.api.android.ui.view.textview.JwTextViewFonts.java
com.markjmind.mobile.api.android.ui.view.textview.JwTextViewWrapScale.java
com.markjmind.mobile.api.android.ui.wrapper.JwAlertDialog.java
com.markjmind.mobile.api.android.ui.wrapper.JwToast.java
com.markjmind.mobile.api.android.util.JwFile.java
com.markjmind.mobile.api.android.util.JwPreferenceList.java
com.markjmind.mobile.api.android.util.JwPreference.java
com.markjmind.mobile.api.android.util.JwSound.java
com.markjmind.mobile.api.android.util.JwTextUtils.java
com.markjmind.mobile.api.android.util.PhoneInfo.java
com.markjmind.mobile.api.android.util.WebConnection.java
com.markjmind.mobile.api.android.xml.JwDomHandler.java
com.markjmind.mobile.api.android.xml.JwDomLoader.java
com.markjmind.mobile.api.android.xml.JwEditElementHandler.java
com.markjmind.mobile.api.android.xml.JwXmlLoader.java
com.markjmind.mobile.api.android.xml.JwXmlResoure.java
com.markjmind.mobile.api.hub.ArrayJDhub.java
com.markjmind.mobile.api.hub.DBhub.java
com.markjmind.mobile.api.hub.JDhub.java
com.markjmind.mobile.api.hub.Loader.java
com.markjmind.mobile.api.hub.StoreList.java
com.markjmind.mobile.api.hub.Store.java