将Python Dict迁移到Java和JSON

时间:2021-10-12 18:08:14

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.

我在Python中是一个不错的程序员。现在我*为我的聊天应用程序做一个Wowza模块。将通过facebook帐户登录的应用程序和每个用户的状态保存在Wowza Server上,该服务器使用java进行应用程序开发,通过Flash客户端和RTMP连接。在线状态数据结构在Python中将是这样的。

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

请告诉我如何用Java表示它,我不熟悉java中的变量'Types':(

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.

10001,10002等将是facebook用户ID ..和0,1将是他们的在线/离线状态。如果连接了10001,则数据结构将进行一些修改,它会将10001的状态更改为1,并添加从facebook检索到的所有朋友ID并更新其状态。

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?

并且如果用户10001断开连接,它将转到更早的阶段。无论如何我可以将它存储为json对象吗?或者有没有简单的方法来存储和检索数据?

3 个解决方案

#1


1  

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.

我假设通过存储和检索,您的意思是将其缓存在内存中,因此:(1)创建javabean类来封装数据。 java HashMap非常类似于python字典。你为什么不尝试在java中编写类,就好像它们是python一样,用结果更新你的问题,然后人们可以帮助你处理没有真正python等价的java泛型等细节。 (2)使用其中一个Object < - > JSON映射框架将实例序列化到JSON或从JSON序列化实例。 Gson和Jackson很受欢迎。

#2


0  

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 ;)

这取决于你想要做什么...如果你可以使用像谷歌Gson这样的json库,那么从Java管理JSON是完美的。然后,如果你想自己编写代码并且你只管理整数和字符串,那就不是很困难了......一个Json结构只是一个数组或一个map键/值,其中key是一个String,Value是一个简单的值或者一个复杂的,因此是一个hashmap或一个数组...无论如何,通常,它更容易直接使用GSon;)

#3


0  

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.

我需要的只是http://www.json.org/javadoc/org/json/JSONObject.html库。我可以毫不费力地使用它,就像在每个对象中添加'type'一样,并且不能像在python中一样创建树。

Thanks mandubian and jtoberon :))

谢谢mandubian和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);


    }

}

#1


1  

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.

我假设通过存储和检索,您的意思是将其缓存在内存中,因此:(1)创建javabean类来封装数据。 java HashMap非常类似于python字典。你为什么不尝试在java中编写类,就好像它们是python一样,用结果更新你的问题,然后人们可以帮助你处理没有真正python等价的java泛型等细节。 (2)使用其中一个Object < - > JSON映射框架将实例序列化到JSON或从JSON序列化实例。 Gson和Jackson很受欢迎。

#2


0  

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 ;)

这取决于你想要做什么...如果你可以使用像谷歌Gson这样的json库,那么从Java管理JSON是完美的。然后,如果你想自己编写代码并且你只管理整数和字符串,那就不是很困难了......一个Json结构只是一个数组或一个map键/值,其中key是一个String,Value是一个简单的值或者一个复杂的,因此是一个hashmap或一个数组...无论如何,通常,它更容易直接使用GSon;)

#3


0  

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.

我需要的只是http://www.json.org/javadoc/org/json/JSONObject.html库。我可以毫不费力地使用它,就像在每个对象中添加'type'一样,并且不能像在python中一样创建树。

Thanks mandubian and jtoberon :))

谢谢mandubian和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);


    }

}