문제

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.

도움이 되었습니까?

해결책

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.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top