0
data = data.decode('utf-8')
print(data)   # b'1' : {"reciever": "1", "sender:": 1, "seq_num": 7, "data": "2"}
data = data[7:] 
print(data)   # {"reciever": "1", "sender:": 1, "seq_num": 7, "data": "2"}
data = json.dumps(data)
data = json.loads(data)
print(type(data)) #<class 'str'>

Here is my code. I try to send data (string + json) with socket programming. It sends data well, but I want to use json when receiving messages. So I sliced the json part, and dumps and loads into json. (I imported json) But Its type is still in str.

In my sending function,

result = json.dumps({'reciever' : rec, 'sender:': sender, 'seq_num' : sequence_number, 'data': data})
final_result = json.loads(result)
print(type(final_result)). #dict type

It seems works well here.

Please help !

Thank you

1 Answer 1

2

remove data = json.dumps(data).

json.dumps saves json object to str. but data is already str in your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.