1

I am using snmp4j to do a SNMP Walk result needs to be sent to a client to decode in python for further analysis. I am new to json and need to help in knowing best ways to convert java datatypes to json so that it can be easily decoded in python. I am not sure if it can be decoded in proper dicts or lists but any help in doing so will be useful. Currently I am using gson for converting below resonse to json

[vbs=[1.3.6.1.2.1.2.2.1.2.1 = VLAN1, 1.3.6.1.2.1.2.2.1.2.2 = FastEthernet0/1],status=0,exception=null,report=null]

and below is result when i do json.loads

[{u'vbs': [{u'variable': {u'value': [86, 76, 65, 78, 49]}, u'oid': {u'value': [1, 3, 6, 1, 2, 1, 2, 2, 1, 2, 1]}}, {u'variable': {u'value': [70, 97, 115, 116, 69, 116, 104, 101, 114, 110, 101, 116, 48, 47, 49]}, u'oid': {u'value': [1, 3, 6, 1, 2, 1, 2, 2, 1, 2, 2]}}], u'status': 0}]

please help in understanding better ways to encode into json for easy and usable python decodes. Is java serialization required here? I am not sure what it means but still wondering if my question is clear, any help at least to point me right resources will be of great help.

RAFIQ
  • 905
  • 3
  • 18
  • 32
  • your question is probably: how do I create a custom json representation of an object using gson (in Java). The Python part is always just `json.loads()` -- nothing interesting. Of course, you can also postprocess the json in Python to achieve the same result: walk the result dict and convert it to whatever structure you like. – jfs May 09 '14 at 12:08
  • post-processing is not wise if we can structure it well into json before sending, as it needs to scale. Yes I am using gson but really not sure whether there are standard ways to encode or i need a custom one. – RAFIQ May 09 '14 at 13:35
  • That is why I've suggested it as the last choice. You have the option: whether or not it works in your particular case is upto you. If Java is the only producer and the Python code is the only consumer then it might be much easier to manipulate the data in Python then in Java (your case may be different) – jfs May 09 '14 at 13:39
  • Yes, Java is the only producer and Python is the only consumer, will manipulate in python unless there are better inputs, Thanks – RAFIQ May 09 '14 at 13:47
  • the *first* option is to create the custom json representation using gson – jfs May 09 '14 at 13:50
  • I am trying that currently, please do help if you have any examples to do so. – RAFIQ May 09 '14 at 13:56

1 Answers1

0

Good solution is using HashMap in java which when converted to json can be easily decoded as dictionary without any manipulation. So here is how I did

//declare a hashmap
HashMap hm = new HashMap();

//loop over the snmp result
for (VariableBinding varBinding : varBindings) {
    hm.put(varBinding.getOid(), varBinding.getVariable().toString());
String js = new Gson().toJson(hm);
//now if you return js as string and do json.loads(js) in python you will get dictionary with oid as key and value as value for all oids.
RAFIQ
  • 905
  • 3
  • 18
  • 32