| 1 | 
 package net.oni2.moddepot; | 
 
 
 
 
 
 | 2 | 
  | 
 
 
 
 
 
 | 3 | 
 import org.json.JSONException; | 
 
 
 
 
 
 | 4 | 
 import org.json.JSONObject; | 
 
 
 
 
 
 | 5 | 
  | 
 
 
 
 
 
 | 6 | 
 /** | 
 
 
 
 
 
 | 7 | 
  * @author Christian Illy | 
 
 
 
 
 
 | 8 | 
  */ | 
 
 
 
 
 
 | 9 | 
 public class JSONHelpers { | 
 
 
 
 
 
 | 10 | 
         /** | 
 
 
 
 
 
 | 11 | 
          * Return the string associated with the given key or null if the key does | 
 
 
 
 
 
 | 12 | 
          * not exist or does not contain a string | 
 
 
 
 
 
 | 13 | 
          *  | 
 
 
 
 
 
 | 14 | 
          * @param parent | 
 
 
 
 
 
 | 15 | 
          *            Parent JSON object | 
 
 
 
 
 
 | 16 | 
          * @param key | 
 
 
 
 
 
 | 17 | 
          *            Key to look for | 
 
 
 
 
 
 | 18 | 
          * @return Contained string or null | 
 
 
 
 
 
 | 19 | 
          */ | 
 
 
 
 
 
 | 20 | 
         public static String getStringOrNull(JSONObject parent, String key) { | 
 
 
 
 
 
 | 21 | 
                 try { | 
 
 
 
 
 
 | 22 | 
                         Object obj = parent.get(key); | 
 
 
 
 
 
 | 23 | 
                         if (obj instanceof String) | 
 
 
 
 
 
 | 24 | 
                                 return (String) obj; | 
 
 
 
 
 
 | 25 | 
                 } catch (JSONException e) { | 
 
 
 
 
 
 | 26 | 
                         e.printStackTrace(); | 
 
 
 
 
 
 | 27 | 
                 } | 
 
 
 
 
 
 | 28 | 
                 return null; | 
 
 
 
 
 
 | 29 | 
         } | 
 
 
 
 
 
 | 30 | 
 } |