Fisher Coder
Fisher Coder

Reputation: 3576

How to extract one substring from one Java Object in python?

I've a String like this:

["[Ljava.lang.Object;",[["Object0",{"key0": null, "coolKey1": "coolValue", "notCoolKey2":"something"}],["Object1",{"key3": "value1", "key4": "nonCoolValue", "Id":"importantId0", "someId":"importantId1"}],false,["Object3",{"key6": "value6", "nonkey7": "supercoolValue"}]]]

what I'm interested is these two key-value pairs:
Id:importantId0 and someId:importantId1

How can I extract these two key value pairs from this String in Python?

I've tried to use STRING.spit("DELIMITER"), but failed, any help is greatly appreciated!

Upvotes: 0

Views: 122

Answers (1)

kindall
kindall

Reputation: 184280

That looks to be a JSON serialization and so you should use the json module to convert it to a Python object, then access the data you need from it.

import json

x = json.loads("""["[Ljava.lang.Object;",[["Object0",{"key0": null, "coolKey1": "coolValue", 
"notCoolKey2":"something"}],["Object1",{"key3": "value1", "key4": "nonCoolValue", 
"Id":"importantId0", "someId":"importantId1"}],false,["Object3", {"key6": "value6", 
"nonkey7": "supercoolValue"}]]]""")

print x[1][1][1]["Id"]
print x[1][1][1]["someId"]

Upvotes: 1

Related Questions