几道题

import com.google.gson.JsonObject;

public class JSONUtils
{

    public JSONUtils()
    {}

    public static JsonObject conversion(JsonObject jsonObject)
    {
        JsonObject returnJsonObject = new JsonObject();
        toJSONString(returnJsonObject, jsonObject, "");
        return returnJsonObject;
    }

    private static void toJSONString(JsonObject returnJsonObject, JsonObject jsonObject,
                                     String objectKey)
    {

        if (jsonObject != null)
        {
            jsonObject.entrySet().stream().forEach(entry -> {
                if (entry.getValue().isJsonObject())
                {
                    toJSONString(returnJsonObject, (JsonObject)entry.getValue(), entry.getKey());
                }
                else
                {
                    returnJsonObject.add(
                        "".equals(objectKey) ? entry.getKey() : (objectKey + "." + entry.getKey()),
                        entry.getValue());
                }
            });
        }
    }

}

你可能感兴趣的:(几道题)