All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yy.httpproxy.service.PushedNotification Maven / Gradle / Ivy

There is a newer version: 1.0.45
Show newest version
package com.yy.httpproxy.service;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by xuduo on 11/6/15.
 */
public class PushedNotification {

    public HashMap values;
    public String id;

    public PushedNotification(String id, JSONObject object) {
        this.id = id;
        values = jsonToMap(object);
    }

    public static HashMap jsonToMap(JSONObject json) {
        if (json != JSONObject.NULL) {
            try {
                return toMap(json);
            } catch (JSONException e) {

            }
        }
        return new HashMap<>();
    }

    public static HashMap toMap(JSONObject object) throws JSONException {
        HashMap map = new HashMap();

        Iterator keysItr = object.keys();
        while (keysItr.hasNext()) {
            String key = keysItr.next();
            Object value = object.get(key);

            if (value instanceof JSONArray) {
                value = toList((JSONArray) value);
            } else if (value instanceof JSONObject) {
                value = toMap((JSONObject) value);
            }
            map.put(key, value);
        }
        return map;
    }

    public static List toList(JSONArray array) throws JSONException {
        List list = new ArrayList();
        for (int i = 0; i < array.length(); i++) {
            Object value = array.get(i);
            if (value instanceof JSONArray) {
                value = toList((JSONArray) value);
            } else if (value instanceof JSONObject) {
                value = toMap((JSONObject) value);
            }
            list.add(value);
        }
        return list;
    }
}