Search code examples
javaandroidjsonparse-platform

Print ParseObject content


perhaps this is a silly question but, I'm trying just to print ALL the content (not just one field) of a ParseObject (Parse platform). But I didn't found anything about that, either the API, forums or here.

toString method it's not overridden inside the API, and there is no "toJsonString" or "toJson" or "getValues" nothing like that.

Any suggestion?

Cheers.


Solution

  • Unfortunately, there is no methods provided by `Parse' to do that. But you can use keySet() method to implement that. It's quick sample to give your direction.

        try {
            TestParse parseObject = ParseObject.createWithoutData(TestParse.class, "tjkbdde0B1");
            JSONObject jsonObject = parseObjectToJson(parseObject);
            Log.d("TAG", jsonObject.toString(4));
        } catch (ParseException | JSONException e) {
            Log.e("TAG", e.getMessage());
        } 
    

    Method to convert to JSON object

    private JSONObject parseObjectToJson(ParseObject parseObject) throws ParseException, JSONException {
        JSONObject jsonObject = new JSONObject();
        parseObject.fetchIfNeeded();
        Set<String> keys = parseObject.keySet();
        for (String key : keys) {
            Object objectValue = parseObject.get(key);
            if (objectValue instanceof ParseObject) {
                jsonObject.put(key, parseObjectToJson(parseObject.getParseObject(key)));
                // keep in mind about "pointer" to it self, will gain stackoverlow
            } else if (objectValue instanceof ParseRelation) {
                // handle relation
            } else {
                jsonObject.put(key, objectValue.toString());
            }
        }
        return jsonObject;
    }
    

    You can extend it to care about other possible types at Parse, all in your hands.