Android Open Source - plus1-android-sdk Json Helper






From Project

Back to project page plus1-android-sdk.

License

The source code is released under:

Copyright (c) 2012, WapStart All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Red...

If you think the Android project plus1-android-sdk 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 ru.wapstart.plus1.sdk;
/*  w w w .j a  v  a 2 s . c o m*/
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.util.*;
 
public class JsonHelper {
  public static Object toJSON(Object object) throws JSONException {
    if (object instanceof Map) {
      JSONObject json = new JSONObject();
      Map map = (Map) object;
      for (Object key : map.keySet()) {
        json.put(key.toString(), toJSON(map.get(key)));
      }
      return json;
    } else if (object instanceof Iterable) {
      JSONArray json = new JSONArray();
      for (Object value : ((Iterable)object)) {
        json.put(value);
    }
      return json;
    } else {
      return object;
    }
  }

  public static boolean isEmptyObject(JSONObject object) {
    return object.names() == null;
  }

  public static Map<String, Object> getMap(JSONObject object, String key) throws JSONException {
    return toMap(object.getJSONObject(key));
  }

  public static Map<String, Object> toMap(JSONObject object) throws JSONException {
    Map<String, Object> map = new HashMap();
    Iterator keys = object.keys();
    while (keys.hasNext()) {
      String key = (String) keys.next();
      map.put(key, fromJson(object.get(key)));
    }
    return map;
  }

  public static List toList(JSONArray array) throws JSONException {
    List list = new ArrayList();
    for (int i = 0; i < array.length(); i++) {
      list.add(fromJson(array.get(i)));
    }
    return list;
  }

  private static Object fromJson(Object json) throws JSONException {
    if (json == JSONObject.NULL) {
      return null;
    } else if (json instanceof JSONObject) {
      return toMap((JSONObject) json);
    } else if (json instanceof JSONArray) {
      return toList((JSONArray) json);
    } else {
      return json;
    }
  }
}




Java Source Code List

ru.wapstart.plus1.bart.BartActivity.java
ru.wapstart.plus1.sdk.AdView.java
ru.wapstart.plus1.sdk.ApplicationBrowser.java
ru.wapstart.plus1.sdk.Base64.java
ru.wapstart.plus1.sdk.BaseAdView.java
ru.wapstart.plus1.sdk.BaseRequestLoader.java
ru.wapstart.plus1.sdk.Constants.java
ru.wapstart.plus1.sdk.HtmlBannerDownloader.java
ru.wapstart.plus1.sdk.InitRequestLoader.java
ru.wapstart.plus1.sdk.JsonHelper.java
ru.wapstart.plus1.sdk.MraidAbstractController.java
ru.wapstart.plus1.sdk.MraidBrowserController.java
ru.wapstart.plus1.sdk.MraidCommandRegistry.java
ru.wapstart.plus1.sdk.MraidCommand.java
ru.wapstart.plus1.sdk.MraidDisplayController.java
ru.wapstart.plus1.sdk.MraidProperty.java
ru.wapstart.plus1.sdk.MraidView.java
ru.wapstart.plus1.sdk.Plus1AdAnimator.java
ru.wapstart.plus1.sdk.Plus1BannerAsker.java
ru.wapstart.plus1.sdk.Plus1BannerDownloadListener.java
ru.wapstart.plus1.sdk.Plus1BannerView.java
ru.wapstart.plus1.sdk.Plus1Helper.java
ru.wapstart.plus1.sdk.Plus1Request.java