Example usage for org.json JSONObject NULL

List of usage examples for org.json JSONObject NULL

Introduction

In this page you can find the example usage for org.json JSONObject NULL.

Prototype

Object NULL

To view the source code for org.json JSONObject NULL.

Click Source Link

Document

It is sometimes more convenient and less ambiguous to have a NULL object than to use Java's null value.

Usage

From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java

private Map<String, List<String>> extractRawAttributesFromJSON(final JSONObject json) {
    Map<String, List<String>> rawAttributes = new HashMap<>();
    for (Object rawKey : json.keySet()) {
        String key = (String) rawKey;
        JSONArray valueArr = (JSONArray) json.get(key);
        int stringCount = valueArr.length();
        List<String> values = new ArrayList<String>(stringCount);
        for (int i = 0; i < stringCount; ++i) {
            Object value = valueArr.get(i);
            if (value.equals(JSONObject.NULL)) {
                values.add(null);/*from   w  ww  . j ava  2 s.c om*/
            } else {
                values.add(value.toString());
            }
        }
        rawAttributes.put(key, values);
    }
    return rawAttributes;
}

From source file:fi.elfcloud.sci.container.Cluster.java

public Cluster(Client client, JSONObject object) throws JSONException {
    this.client = client;
    this.id = object.getInt("id");
    this.name = object.getString("name");
    this.childCount = object.getInt("descendants");
    this.dataItemCount = object.getInt("dataitems");
    this.parent_id = object.getInt("parent_id");
    this.last_accessed_date = (object.get("last_accessed_date") != JSONObject.NULL
            ? object.getString("last_accessed_date")
            : "");
    this.last_modified_date = (object.get("modified_date") != JSONObject.NULL
            ? object.getString("modified_date")
            : "");
    this.permissions = object.getJSONArray("permissions");
}

From source file:com.skysql.manager.api.NodeInfo.java

/**
 * Save node info to API.//from   ww  w  .java2s . c o m
 *
 * @return true, if successful
 */
public boolean save() {

    APIrestful api = new APIrestful();
    boolean success = false;

    try {
        if (ID != null) {
            JSONObject jsonParam = new JSONObject();
            jsonParam.put("name", this.name);
            jsonParam.put("hostname", this.hostname);
            jsonParam.put("instanceid", this.instanceID);
            jsonParam.put("publicip", this.publicIP);
            jsonParam.put("privateip", this.privateIP);
            if (this.dbUsername != null) {
                jsonParam.put("dbusername", this.dbUsername);
            }
            if (this.dbPassword != null) {
                jsonParam.put("dbpassword", this.dbPassword != null ? this.dbPassword : JSONObject.NULL);
            }
            if (this.repUsername != null) {
                jsonParam.put("repusername", this.repUsername);
            }
            if (this.repPassword != null) {
                jsonParam.put("reppassword", this.repPassword != null ? this.repPassword : JSONObject.NULL);
            }
            success = api.put("system/" + parentID + "/node/" + ID, jsonParam.toString());
        } else {
            StringBuffer regParam = new StringBuffer();
            regParam.append("name=" + URLEncoder.encode(this.name, "UTF-8"));
            regParam.append("&hostname=" + URLEncoder.encode(this.hostname, "UTF-8"));
            regParam.append("&instanceid=" + URLEncoder.encode(this.instanceID, "UTF-8"));
            regParam.append("&publicip=" + URLEncoder.encode(this.publicIP, "UTF-8"));
            regParam.append("&privateip=" + URLEncoder.encode(this.privateIP, "UTF-8"));
            if (this.dbUsername != null) {
                regParam.append("&dbusername=" + URLEncoder.encode(this.dbUsername, "UTF-8"));
            }
            if (this.dbPassword != null) {
                regParam.append("&dbpassword=" + URLEncoder.encode(this.dbPassword, "UTF-8"));
            }
            if (this.repUsername != null) {
                regParam.append("&repusername=" + URLEncoder.encode(this.repUsername, "UTF-8"));
            }
            if (this.repPassword != null) {
                regParam.append("&reppassword=" + URLEncoder.encode(this.repPassword, "UTF-8"));
            }
            success = api.post("system/" + parentID + "/node", regParam.toString());
        }

    } catch (JSONException e) {
        new ErrorDialog(e, "Error encoding API request");
        throw new RuntimeException("Error encoding API request");
    } catch (UnsupportedEncodingException e) {
        new ErrorDialog(e, "Error encoding API request");
        throw new RuntimeException("Error encoding API request");
    }

    if (success) {
        WriteResponse writeResponse = APIrestful.getGson().fromJson(api.getResult(), WriteResponse.class);
        if (writeResponse != null && ID == null && !writeResponse.getInsertKey().isEmpty()) {
            ID = writeResponse.getInsertKey();
            return true;
        } else if (writeResponse != null && ID != null && writeResponse.getUpdateCount() > 0) {
            return true;
        }
    }

    return false;

}

From source file:org.projectbuendia.client.models.Order.java

public JSONObject toJson() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("patient_uuid", patientUuid);
    json.put("instructions", instructions);
    json.put("start_millis", start.getMillis());
    // Use `JSONObject.NULL` instead of `null` so that the value is actually set.
    json.put("stop_millis", stop == null ? JSONObject.NULL : stop.getMillis());
    return json;//from   w ww  .j  ava 2s  .  co m
}

From source file:com.abc.driver.MainActivity.java

/**
 * ??? /*from w  ww .  j a  v  a2s.c  o m*/
 * {"result_code":"0","horders":[{"id":6,"shipper_username":
 * "\u674e\u674e\u90bb\u5c45\u5929",
 * "shipper_phone":"","shipper_date":"2015-03-15 00:00:00"
 * ,"shipper_address_code":"110000-110100-110101",
 * "consignee_username":"","consignee_phone"
 * :"","consignee_address_code":"110000-110100-110101",
 * "delivery_time":"0000-00-00 00:00:00"
 * ,"truck_type":"","truck_length":"","cargo_type":"CT1",
 * "cargo_volume":"\u6d4b\u8bd5",
 * "cargo_weight":"\u6d4b\u8bd5\u554a","horder_desc"
 * :"","user_id":9,"status":0, "created_at":"2015-03-15 03:30:50",
 * "updated_at":"2015-03-15 03:30:50"},}]}
 * 
 * @param jsonResult
 */
public void parseJson(JSONObject jsonResult) {
    HashMap<String, Object> mHorder;
    try {
        if (jsonResult.get("horders") != JSONObject.NULL) {
            JSONArray results = jsonResult.getJSONArray("horders");
            if (results.length() < CellSiteConstants.PAGE_COUNT) {
                mHorderTypes[mCurrRadioIdx].hasShowAllHorders = true;
            }

            for (int i = 0; i < results.length(); i++) {
                try {
                    JSONObject resultObj = (JSONObject) results.get(i);
                    mHorder = new HashMap<String, Object>();
                    mHorder.put(CellSiteConstants.SHIPPER_USERNAME,
                            resultObj.getString(CellSiteConstants.SHIPPER_USERNAME));
                    mHorder.put("horder_id", (resultObj).getString(CellSiteConstants.ID));
                    // TODO : 
                    int counter = 0;

                    mHorderTypes[mCurrRadioIdx].nHorders.add(mHorder);
                } catch (Exception e) {
                    mHasExceptionHorder = true;
                    continue;
                }

            }

        }

    } catch (JSONException e) {
        Log.d(TAG, "JSONException" + e.toString());

    }

}

From source file:com.abc.driver.MainActivity.java

/**
 * ??? /*from ww w . j  av  a 2  s  .  co m*/
 * {"result_code":"0","horders":[{"id":6,"shipper_username":
 * "\u674e\u674e\u90bb\u5c45\u5929",
 * "shipper_phone":"","shipper_date":"2015-03-15 00:00:00"
 * ,"shipper_address_code":"110000-110100-110101",
 * "consignee_username":"","consignee_phone"
 * :"","consignee_address_code":"110000-110100-110101",
 * "delivery_time":"0000-00-00 00:00:00"
 * ,"truck_type":"","truck_length":"","cargo_type":"CT1",
 * "cargo_volume":"\u6d4b\u8bd5",
 * "cargo_weight":"\u6d4b\u8bd5\u554a","horder_desc"
 * :"","user_id":9,"status":0, "created_at":"2015-03-15 03:30:50",
 * "updated_at":"2015-03-15 03:30:50"},}]}
 * 
 * @param jsonResult
 */
public void parseFHorderJson(JSONObject jsonResult) {
    HashMap<String, Object> mHorder;
    try {
        if (jsonResult.get("horders") != JSONObject.NULL) {
            JSONArray results = jsonResult.getJSONArray("horders");
            if (results.length() < CellSiteConstants.PAGE_COUNT) {
                mFHorderTypes.hasShowAllHorders = true;
            }

            for (int i = 0; i < results.length(); i++) {
                try {
                    JSONObject resultObj = (JSONObject) results.get(i);
                    JSONArray repliedDriversObj = null;
                    mHorder = new HashMap<String, Object>();

                    mHorder.put(CellSiteConstants.ALREADY_REPLIED, 0);
                    try {
                        // 1. ???repliedtag 1 ?0
                        // 2.
                        repliedDriversObj = resultObj.getJSONArray(CellSiteConstants.REPLIED_DRIVERS);
                        if (repliedDriversObj != null) {
                            for (int j = 0; j < repliedDriversObj.length(); j++) {
                                String driver_id = ((JSONObject) repliedDriversObj.get(i))
                                        .getString(CellSiteConstants.DRIVER_ID);
                                if (driver_id.equals("" + app.getUser().getId())) {
                                    mHorder.put(CellSiteConstants.ALREADY_REPLIED, 1);
                                    break;
                                }

                            }
                            mHorder.put(CellSiteConstants.REPLIED_DRIVERS_COUNT, repliedDriversObj.length());
                        }
                    } catch (Exception e) {

                    }

                    mHorder.put(CellSiteConstants.SHIPPER_USERNAME,
                            resultObj.getString(CellSiteConstants.SHIPPER_USERNAME));
                    mHorder.put(CellSiteConstants.HORDER_ID, (resultObj).getString(CellSiteConstants.ID));
                    mHorder.put(CellSiteConstants.SHIPPER_PHONE,
                            (resultObj).getString(CellSiteConstants.SHIPPER_PHONE));
                    mHorder.put(CellSiteConstants.USER_ID, (resultObj).getString(CellSiteConstants.USER_ID));

                    CityDBReader dbReader = new CityDBReader(this.getApplicationContext());
                    mHorder.put(CellSiteConstants.SHIPPER_ADDRESS_NAME, dbReader
                            .getNameFromCode((resultObj).getString(CellSiteConstants.SHIPPER_ADDRESS_CODE_IN)));
                    mHorder.put(CellSiteConstants.CONSIGNEE_ADDRESS_NAME, dbReader
                            .getNameFromCode((resultObj).getString(CellSiteConstants.CONSIGNEE_ADDRESS_CODE2)));
                    mHorder.put(CellSiteConstants.CARGO_TYPE,
                            (resultObj).getString(CellSiteConstants.CARGO_TYPE));
                    mHorder.put(CellSiteConstants.CARGO_WEIGHT,
                            (resultObj).getString(CellSiteConstants.CARGO_WEIGHT));
                    mHorder.put(CellSiteConstants.CARGO_VOLUME,
                            (resultObj).getString(CellSiteConstants.CARGO_VOLUME));
                    mHorder.put(CellSiteConstants.TRUCK_TYPE,
                            (resultObj).getString(CellSiteConstants.TRUCK_TYPE));
                    mHorder.put(CellSiteConstants.HORDER_STATUS,
                            (resultObj).getString(CellSiteConstants.STATUS));
                    mHorder.put(CellSiteConstants.SHIPPER_DATE,
                            (resultObj).getString(CellSiteConstants.SHIPPER_DATE));
                    mHorder.put(CellSiteConstants.HORDER_DESCRIPTION,
                            (resultObj).getString(CellSiteConstants.HORDER_DESCRIPTION));
                    mHorder.put(CellSiteConstants.SHIPPER_USERNAME,
                            (resultObj).getString(CellSiteConstants.SHIPPER_USERNAME));

                    // TODO :
                    int counter = 0;

                    mFHorderTypes.nHorders.add(mHorder);
                } catch (Exception e) {
                    mHasExceptionFHorder = true;
                    continue;
                }

            }

        }

    } catch (JSONException e) {
        Log.d(TAG, "JSONException" + e.toString());

    }

}

From source file:com.orange.mmp.api.ws.jsonrpc.SimpleJSONSerializer.java

@Override
public Object marshall(SerializerState state, Object parent, Object java, Object ref) throws MarshallException {
    if (java == null || java.equals(null)) {
        return JSONObject.NULL;
    } else// w ww .  j  a  v  a 2s. c o m
        return super.marshall(state, parent, java, ref);
}

From source file:com.abc.driver.PersonalActivity.java

public void parseJson(JSONObject jsonResult) {
    JSONObject profileJson = null;//from w  w w  .  ja v  a  2 s. c  o  m
    try {
        try {
            profileJson = jsonResult.getJSONObject(CellSiteConstants.PROFILE);
        } catch (Exception e) {
            profileJson = null;
        }
        JSONObject userJson = jsonResult.getJSONObject(CellSiteConstants.USER);
        if (profileJson != null) {
            if (profileJson.get(CellSiteConstants.PROFILE_IMAGE_URL) != JSONObject.NULL) {
                Log.d(TAG, "get the image url");
                app.getUser().setProfileImageUrl(profileJson.getString(CellSiteConstants.PROFILE_IMAGE_URL));

            }
            if (profileJson.get(CellSiteConstants.DRIVER_LICENSE_URL) != JSONObject.NULL) {
                app.getUser()
                        .setDriverLicenseImageUrl(profileJson.getString(CellSiteConstants.DRIVER_LICENSE_URL));
            }
            if (profileJson.get(CellSiteConstants.IDENTITY_CARD_IMAGE_URL) != JSONObject.NULL) {
                app.getUser()
                        .setIdentityImageUrl(profileJson.getString(CellSiteConstants.IDENTITY_CARD_IMAGE_URL));
            }

            if (profileJson.get(CellSiteConstants.NAME) != JSONObject.NULL) {
                app.getUser().setName(profileJson.getString(CellSiteConstants.NAME));
            }

        }
        if (userJson.get(CellSiteConstants.MOBILE) != JSONObject.NULL) {
            app.getUser().setMobileNum(userJson.getString(CellSiteConstants.MOBILE));
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

From source file:com.parse.simple.SimpleParse.java

public synchronized static ParseObject commit(Object from, ParseObject to) {
    for (Map.Entry<SimpleField, SimpleParseColumn> fieldEntry : SimpleParseCache.get()
            .getColumnFields(from.getClass()).entrySet()) {
        final SimpleField field = fieldEntry.getKey();
        final SimpleParseColumn column = fieldEntry.getValue();
        final String columnName = SimpleParseCache.get().getColumnName(field, column);

        if (TextUtils.isEmpty(columnName))
            continue;

        Bundle icicle = SimpleParseCache.get().columnDataCache.get(columnName);
        if (icicle == null) {
            icicle = new Bundle();
            SimpleParseCache.get().columnDataCache.put(columnName, icicle);
        }// w  w  w  .j a  v  a 2 s  . com

        Class<?> fieldType = field.getType();
        try {
            Object value = field.get(from);

            Class<? extends Filter> filter = column.filter();
            if (!Optional.class.equals(column.saver())) {
                value = ((Value) SimpleParseCache.get().getObject(column.saver())).value();
            } else if (NullValue.class.equals(column.saver())) {
                continue;
            } else if (!OptionalFilter.class.equals(filter)) {
                value = SimpleParseCache.get().getFilter(filter).onSave(value, icicle, from, to);
            }

            if (column.self() && fieldType.isAssignableFrom(ParseObject.class)) {
                // do nothing
            } else if (value == null) {
                to.put(columnName, JSONObject.NULL);
                //to.remove(columnName);
            } else if (fieldType.equals(Byte.class) || fieldType.equals(byte.class)) {
                to.put(columnName, (Byte) value);
            } else if (fieldType.equals(Short.class) || fieldType.equals(short.class)) {
                to.put(columnName, (Short) value);
            } else if (fieldType.equals(Integer.class) || fieldType.equals(int.class)) {
                to.put(columnName, (Integer) value);
            } else if (fieldType.equals(Long.class) || fieldType.equals(long.class)) {
                to.put(columnName, (Long) value);
            } else if (fieldType.equals(Float.class) || fieldType.equals(float.class)) {
                to.put(columnName, (Float) value);
            } else if (fieldType.equals(Double.class) || fieldType.equals(double.class)) {
                to.put(columnName, (Double) value);
            } else if (fieldType.equals(Boolean.class) || fieldType.equals(boolean.class)) {
                to.put(columnName, (Boolean) value);
            } else if (fieldType.equals(Character.class) || fieldType.equals(char.class)) {
                to.put(columnName, value.toString());
            } else if (fieldType.equals(String.class)) {
                String valueString = value.toString();
                String prefix = column.prefix();
                String suffix = column.suffix();

                Class<?> prefixClass = column.prefixClass();
                if (!Optional.class.equals(prefixClass)) {
                    prefix = (String) ((Value) SimpleParseCache.get().getObject(prefixClass)).value();
                }

                Class<?> suffixClass = column.suffixClass();
                if (!Optional.class.equals(suffixClass)) {
                    suffix = (String) ((Value) SimpleParseCache.get().getObject(suffixClass)).value();
                }

                if (!TextUtils.isEmpty(prefix) && valueString.startsWith(prefix)) {
                    //valueString.replace(prefix, "");
                    valueString = valueString.substring(prefix.length(), valueString.length());
                }

                if (!TextUtils.isEmpty(suffix) && valueString.endsWith(suffix)) {
                    valueString = valueString.substring(0, valueString.length() - suffix.length());
                }

                if (SimpleParseObject.OBJECT_ID.equals(columnName)) {
                    to.setObjectId(valueString);
                } else {
                    to.put(columnName, valueString);
                }
            } else if (fieldType.equals(Byte[].class) || fieldType.equals(byte[].class)) {
                to.put(columnName, (byte[]) value);
            } else if (fieldType.equals(JSONObject.class)) {
                to.put(columnName, (JSONObject) value);
            } else if (fieldType.equals(List.class)) {
                to.addAll(columnName, (List) value);
            } else if (fieldType.equals(Date.class)) {
                to.put(columnName, (Date) value);
            } else if (fieldType.equals(ParseUser.class)) {
                to.put(columnName, (ParseUser) value);
            } else if (fieldType.equals(ParseGeoPoint.class)) {
                to.put(columnName, (ParseGeoPoint) value);
            } else if (fieldType.equals(ParseObject.class)) {
                to.put(columnName, (ParseObject) value);
            }
            //else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
            //to.put(columnName, ((Enum<?>) value).name());
            //}
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        }
    }
    SimpleParseCache.get().columnDataCache.clear();
    return to;
}

From source file:run.ace.IncomingMessages.java

public static void set(JSONArray message) throws JSONException {
    Object instance = Handle.deserialize(message.getJSONObject(1));
    String propertyName = message.getString(2);
    Object propertyValue = message.get(3);

    // Convert non-primitives (TODO arrays)
    if (propertyValue instanceof JSONObject) {
        propertyValue = Utils.deserializeObjectOrStruct((JSONObject) propertyValue);
    } else if (propertyValue == JSONObject.NULL) {
        propertyValue = null;//  w ww .  jav  a 2s .  c om
    }

    try {
        if (instance instanceof IHaveProperties) {
            ((IHaveProperties) instance).setProperty(propertyName, propertyValue);
        } else {
            // Try reflection. So XXX.YYY maps to a setYYY method.
            try {
                String setterName = "set";
                if (propertyName.contains(".")) {
                    setterName += propertyName.substring(propertyName.lastIndexOf(".") + 1);
                } else {
                    setterName += propertyName;
                }

                //TODO: Need to do more permissive parameter matching in this case since everything will be strings.
                // Started to add this with looseMatching param. Continue.
                try {
                    Integer value = Integer.parseInt(propertyValue.toString());
                    propertyValue = value;
                } catch (java.lang.NumberFormatException nfe) {
                    try {
                        Double value = Double.parseDouble(propertyValue.toString());
                        propertyValue = value;
                    } catch (java.lang.NumberFormatException nfe2) {
                        // Keep as string
                    }
                }

                // TODO: Enable marshaling of things like "red" to Drawable...

                Utils.invokeMethodWithBestParameterMatch(instance.getClass(), setterName, instance,
                        new Object[] { propertyValue }, true);
            } catch (Exception ex) {
                //
                // Translate standard cross-platform (XAML) properties for well-known base types
                //
                if (instance instanceof TextView) {

                    if (propertyName.endsWith(".Children") && propertyValue instanceof ItemCollection) {
                        // This is from XAML compilation of a custom content property, which always gives an ItemCollection.
                        propertyName = "ContentControl.Content";
                        if (((ItemCollection) propertyValue).size() == 1) {
                            propertyValue = ((ItemCollection) propertyValue).get(0);
                        }
                    }

                    if (!TextViewHelper.setProperty((TextView) instance, propertyName, propertyValue)) {
                        throw new RuntimeException("Unhandled property for a custom TextView: " + propertyName
                                + ". Implement IHaveProperties to support this.");
                    }
                } else if (instance instanceof ViewGroup) {
                    if (propertyName.endsWith(".Children") && propertyValue instanceof ItemCollection) {
                        // This is from XAML compilation of a custom content property, which always gives an ItemCollection.
                        ItemCollection children = (ItemCollection) propertyValue;
                        for (int i = 0; i < children.size(); i++) {
                            ((ViewGroup) instance).addView((View) children.get(i));
                        }
                    } else if (!ViewGroupHelper.setProperty((ViewGroup) instance, propertyName,
                            propertyValue)) {
                        throw new RuntimeException("Unhandled property for a custom ViewGroup: " + propertyName
                                + ". Implement IHaveProperties to support this.");
                    }
                } else if (instance instanceof View) {
                    if (!ViewHelper.setProperty((View) instance, propertyName, propertyValue, true)) {
                        throw new RuntimeException("Unhandled property for a custom View: " + propertyName
                                + ". Implement IHaveProperties to support this.");
                    }
                } else {
                    throw new RuntimeException("Either there must be a set" + propertyName
                            + " method, or IHaveProperties must be implemented.");
                }
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException("Error setting " + instance.getClass().getSimpleName() + "'s " + propertyName
                + " to " + propertyValue, ex);
    }
}