开发者

Migrate Python Dict to Java and JSON

I was a decent programmer in Python. Now i am forced to do a Wowza Module for my chat application. An application which will login by facebook account and the status of each user is saved on a Wowza Server, which use java for app development, connected via flash client & RTMP. The online status datastructure will be like this in Python.

Please tell me how to represent it in Java, I am not so familar with variable 'Types' in java :(

x = {
    10001: {
        'status': 0,
        'friends': {}
    },
    10002: {
        'status': 1,
        'friends': {
            10001: 0,
            10003: 1
        }
    },
    10003: {
        'status': 1,
        'friends': {
            10001: 0,
            10003: 1
        }
    }
}

10001,10002 etc will be facebook user ids.. and 0,1 will be their online/offline status. If 10001 is connected, the datastructure will have some little modifications, it will change the status of 10001 to 1, and add all his friends ids, retrieved from facebook and update their status too.

x = {
    10001: {
        'status': 1,
        'friends': {
            10002: 1,
            10003: 1        
        }
    },
    10002: {
        'status': 1,
        'friends': {
            10001: 1,
            10003: 1
        }
    },
    10003: {
        'status': 1,
        'friends': {
            10001: 1,
            10003: 1
        }
    }
}

And if the user 10001 is disconnected, it will goto earlier stage. Is there anyway i can开发者_开发知识库 store it as a json object? or is there any simple way to Store and retrieve data?


I assume that by store and retrieve, you mean cache it in memory, so: (1) Create javabean classes to encapsulate the data. A java HashMap is very similar to a python dictionary. Why don't you try to write the classes in java as if they were python, update your question with the result, and then people can help you with details such as java generics which have no real python equivalent. (2) Use one of the Object<-->JSON mapping frameworks that are out there to serialize instances to/from JSON. Gson and Jackson are popular.


It depends on what you want to do...
If you can use a json library such as Google Gson, it's perfect to manage JSON from Java. then if you want to code it by yourself and you just manage integers and strings, it's not very difficult...
a Json structure is just an array or a map key/value where key is a String and Value is either a simple value or a complex one hence a hashmap or an array...
Anyway, generally, it's easier to use directly GSon ;)


All i needed was http://www.json.org/javadoc/org/json/JSONObject.html library. I could use it with little struggle like adding 'type' to every object and can't create the tree in one step as in python.

Thanks mandubian and jtoberon :))

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class JSONExample {
    JSONObject json;
    JSONObject objJSON;
    JSONObject objObjJSON;

    public void addtoJSON(){
        json = new JSONObject();
        objJSON= new JSONObject();
        objObjJSON =new JSONObject();

        //adding last tree
        objObjJSON.put(10001, 0);
        objObjJSON.put(10002, 1);

        //adding secondary tree
        objJSON.put("status",1);
        objJSON.put("friends",objObjJSON);

        //added root tree
        objJSON.put(10003,objJSON);

        System.out.println("JSON is " + objJSON);


    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜