Data type conversion for JSON : JSON « Development « Android






Data type conversion for JSON

   
//package org.beryl.util;

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

 class TryParse {

  public static int toInt(String value, int defaultValue) {
    int result;

    try {
      result = Integer.parseInt(value);
    } catch(NumberFormatException e) { result = defaultValue; }

    return result;
  }

  public static String toString(JSONObject json, String name, String defaultValue) {
    String result;
    try {
      result = json.getString(name);
    } catch(JSONException e) { result = defaultValue; }

    return result;
  }

  public static int toInt(JSONObject json, String name, int defaultValue) {
    int result;
    try {
      result = json.getInt(name);
    } catch(JSONException e) { result = defaultValue; }

    return result;
  }

  public static boolean toBoolean(JSONObject json, String name, boolean defaultValue) {
    boolean result;
    try {
      result = json.getBoolean(name);
    } catch(JSONException e) { result = defaultValue; }

    return result;
  }
}

   
    
    
  








Related examples in the same category

1.Parse Json string
2.Upload Image to Flickr with JSON
3.Flickr JSON Location
4.Using JSON
5.Json Client
6.Get List From Json Object
7.Create JSONObject
8.JSON: Get node value,
9.Object to JSON
10.Wikidictionary
11.Browse through Wiktionary content
12.Utility class supporting the Facebook Object.