Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

json - JSONEncoder double jsonify objects extending from a class in python

I want to get a dictionary out of a Python class object. I'm using JSONEncoder to covert the same. However, what I am getting in return is double jsonified object. All i want is a dictionary.

class DocumentOverlay:
    page: int
    top: None
    left: None
    width: None
    height: None
    def __init__(self, page: int, top: None, left: None, width: None, height: None) -> None:
        self.page = page
        self.top = top
        self.left = left
        self.width = width
        self.height = height


class DSAElement:
    primaryDocument: str
    matchesWithDocument: str
    primaryDocumentOverlay: DocumentOverlay
    matchesWithDocumentOverlay: DocumentOverlay
    similarityIndex: float
    isSimilar: bool
    def __init__(self, primaryDocument: str, matchesWithDocument: str, primaryDocumentOverlay: DocumentOverlay, matchesWithDocumentOverlay: DocumentOverlay, similarityIndex: float, isSimilar: bool) -> None:
        self.primaryDocument = primaryDocument
        self.matchesWithDocument = matchesWithDocument
        self.primaryDocumentOverlay = primaryDocumentOverlay
        self.matchesWithDocumentOverlay = matchesWithDocumentOverlay
        self.similarityIndex = similarityIndex
        self.isSimilar = isSimilar


class dsaEncoder(JSONEncoder):
    def default(self, o):
        return o.__dict__

output=[]
for term, tdoc in zip(thumbnail, tdocs):           
            sim = doc.similarity(tdoc)
            if sim>0.99:
                primary_overlay=DocumentOverlay(term['page'], None, None, None, None)
                secondary_overlay=DocumentOverlay(i+1, None, None, None, None)
                dsa_overlay=DSAElement(term['fileName'], item['fileName'], primary_overlay, secondary_overlay, sim, True)
                output.append(dsaEncoder().encode(dsa_overlay))

What i get in return is:

['{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 1, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 1, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9993153342654213, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9983211993255433, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 21, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.997596096801685, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 3, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 3, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9988208175324497, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 4, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 4, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9960270471399298, "isSimilar": true}']

Any help would be highly appreciated on how to get the return dict like this:

[{"primaryDocument": "test.pdf", "matchesWithDocument": "test2.pdf", "primaryDocumentOverlay": {"page": 1, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.92, "isSimilar": true},....]
question from:https://stackoverflow.com/questions/65935011/jsonencoder-double-jsonify-objects-extending-from-a-class-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use json.loads

a = ['{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 1, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 1, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9993153342654213, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9983211993255433, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 21, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.997596096801685, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 3, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 3, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9988208175324497, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 4, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 4, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9960270471399298, "isSimilar": true}']

output = [json.loads(i) for i in a]

output

[{'primaryDocument': 'Aurum1.pdf',
  'matchesWithDocument': 'Aurum2.pdf',
  'primaryDocumentOverlay': {'page': 1,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'matchesWithDocumentOverlay': {'page': 1,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'similarityIndex': 0.9993153342654213,
  'isSimilar': True},
 {'primaryDocument': 'Aurum1.pdf',
  'matchesWithDocument': 'Aurum2.pdf',
  'primaryDocumentOverlay': {'page': 2,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'matchesWithDocumentOverlay': {'page': 2,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'similarityIndex': 0.9983211993255433,
  'isSimilar': True},
 {'primaryDocument': 'Aurum1.pdf',
  'matchesWithDocument': 'Aurum2.pdf',
  'primaryDocumentOverlay': {'page': 21,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'matchesWithDocumentOverlay': {'page': 2,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'similarityIndex': 0.997596096801685,
  'isSimilar': True},
 {'primaryDocument': 'Aurum1.pdf',
  'matchesWithDocument': 'Aurum2.pdf',
  'primaryDocumentOverlay': {'page': 3,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'matchesWithDocumentOverlay': {'page': 3,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'similarityIndex': 0.9988208175324497,
  'isSimilar': True},
 {'primaryDocument': 'Aurum1.pdf',
  'matchesWithDocument': 'Aurum2.pdf',
  'primaryDocumentOverlay': {'page': 4,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'matchesWithDocumentOverlay': {'page': 4,
   'top': None,
   'left': None,
   'width': None,
   'height': None},
  'similarityIndex': 0.9960270471399298,
  'isSimilar': True}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...