Reputation: 674
I am working on a project which has a python wrapper around java code. So, there is a communication between python and java code. (I am aware of jython, but we wanted to keep each implementation separate.)
To talk from python to java, we pass a string like
{
"1-1": {
"Max Capacity (MB)": "393216",
"Current Capacity (MB)": "24576",
"Max Devices": "12",
"Populated": "12"
},
"1-3": {
"Max Capacity (MB)": "262144",
"Current Capacity (MB)": "8192",
"Max Devices": "32",
"Populated": "2"
},
"1-2": {
"Max Capacity (MB)": "393216",
"Current Capacity (MB)": "4096",
"Max Devices": "12",
"Populated": "1"
}
}
which is a dict in python and we pass as a string to java code.
Is there I library in java which does parsing a python dict like string to a java hash-map or equivalent for easy traversal?
Solution:
The json solution worked. Used simplejson in python to convert the dict to json format and java gson library. Here is the java part:
import java.util.Map;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class ReadFromJson {
public static void main(String args[]) {
Type mapOfStringObjectType = new TypeToken<Map<String, Map<String,String>>>() {}.getType();
Gson gson = new Gson();
Map<String, Map<String,String>> obj = gson.fromJson(jsonStringToBeRead, mapOfStringObjectType);
System.out.println(obj);
}
}
Upvotes: 1
Views: 2484
Reputation: 4105
I tried that but I am doing something like
java -dname="TheAboveString"
and retriving it by System.getProperty("name"), so not able to pass a well formed json.. – kmad 15 mins ago
...and why are you passing it as a system property?
Just pipe in the JSON echo "TheAboveString" | java
and dump it from stdin to a JSON parser. This should be a little easier and much safer using the subprocess module.
Upvotes: 1