How to parse data?
-
Hi there!.
Probably someone asked the same thing (or similar) before, but I didn't find any post..
I'm using LoPy + PubNub in a bidirectional way to exchange information. The exchange of information is fine, there is no problem, but I'm not able to decode the information received in the LoPy device.
For instance, from Android I do this to send information:
JSONObject obj = new JSONObject();
obj.put("port", 2);
obj.put("dev_eui", "XXXXXXXXX");
obj.put("payload", Base64.encodeBase64("hello".getBytes("UTF8")));
pubnub.publish().message(obj.toString()).channel("lopy-down");And in my LoPy code, I do this:
rx_pkt = soc.recv(64)Of course I get the information (rx_pkt!=null), but actually I do not know the format or how to decode it. I expect the json string, but I get an ¿hex? string..I tried binascii.hexlify, decode('utf-8'), ubinascii.a2b_base64(rx_pkt)..but nothing works (a2b_base64 gives an error due to the padding).
I have tried just sending plain text ("hello"), instead a json string, but supposedly pubnub only accepts that json-structure to publish the message, because sending plain text I never get the message in the LoPy side.
Any thoughts?.
Thanks in advance and best regards.
-
@bihut it would probably help if you posted what you currently receive.
-
@jcaron sure!
Sending ("hello "+timestamp) in the payload field of the the json-string, in the LoPy side I have this:
data = s.recv(64)
print ("1",data.decode('utf-8'));
print("2",data)
print("3",ubinascii.hexlify(data))getting:
1 <weird symbols - not readable>
2 b'\x07f\xdc\xd5\xd7'
3 b'0766dcd5d738'Thanks!
-
@bihut said in How to parse data?:
b'0766dcd5d738'
Will be better if you show us what you really sending whole frame not something cutted in the middle
show what you got, whole json and also json sended on Android side - will be helpfullPS. i suppose point 2 is really as below?
b'\x07f\xdc\xd5\xd78' -
Ok, I found the error. I was sending the payload as Base64 bytes, and should be as Base64 as String, that is,
From:
JSONObject obj = new JSONObject();
obj.put("port", 2);
obj.put("dev_eui", "XXXXXX");
obj.put("payload", Base64.encodeBase64(msg.getBytes("UTF8")));To:
JSONObject obj = new JSONObject();
obj.put("port", 2);
obj.put("dev_eui", "XXXXXX");
obj.put("payload", new String(Base64.encodeBase64(msg.getBytes("UTF8"))));Now I get the "hello" =).
Thanks you very much!
-
Is it possible to build reliable app with parse alternative like Back4app ?
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login