簡體   English   中英

使用 C# 將 dynamodb json 轉換為普通 json

[英]Convert dynamodb json to normal json using C#

我正在嘗試使用 C# 將 dynamodb json 轉換為普通 json,實際上對於對象類型 M 我無法實現轉換仍然得到如下結果

{
    "RecordDetails": {
        "firstName": {
            "Value": “32432-ere”,
            "Type": 0
        },
        "lastName": {
            "Value": "32432-ere",
            "Type": 0
        },
        "contactInformation": {
            "addressLine1": {
                "Value": "10 Downing st",
                "Type": 0
            },
            "addressLine2": {
                "Value": "",
                "Type": 0
            },
            "city": {
                "Value": "Portand",
                "Type": 0
            },
            "phone": {
                "Value": "555-555-5555",
                "Type": 0
            },
            "email": {
                "Value": "jdoe@abc.com”,
                "Type": 0
            }
        },
        "employer": {
            “Id”: {
                "Value": “test:1473969",
                "Type": 0
            }
        },
        "middleName": {
            "Value": "A",
            "Type": 0
        },
        "id": {
            "Value": "637934995361318864",
            "Type": 0
        },
        "title": {
            "Value": "Dr.",
            "Type": 0
        },
        "userId": {
            "Value": “ererew-2332432”,
            "Type": 0
        },
        "earnedDegrees": {
            "Value": "MD.",
            "Type": 0
        }
    },
    "ValidationStatus": {
        "Value": "Passed",
        "Type": 0
    },
    "TypeOfRecord": {
        "Value": “test”,
        "Type": 0
    },
    "TenantCode": {
        "Value": “demo2”,
        "Type": 0
    },
    "Action": {
        "Value": "create",
        "Type": 0
    },
    "TenantHostName": {
        "Value": “t1.demo.com",
        "Type": 0
    },
    "ImportJobId": {
        "Value": “erewre7777”-322h23h,
        "Type": 0
    },
    "ValidationMessage": {
        "Value": "No Errors",
        "Type": 0
    },
    "ProcessingResult": {
        "Value": "Fail",
        "Type": 0
    },
    "ProcessedAt": {
        "Value": "2022-07-20T10:07:43.650Z",
        "Type": 0
    },
    "RecordId": {
        "Value": “dfdsf-3242-sdfds”,
        "Type": 0
    },
    "ApiResponse": {
        "errors": {
            "Entries": [
                {
                    "code": {
                        "Value": "-1",
                        "Type": 1
                    },
                    "internalErrorMessage": {
                        "Value": "",
                        "Type": 0
                    },
                    "links": {
                        "about": {
                            "Value": "",
                            "Type": 0
                        },
                        "help": {
                            "Value": "",
                            "Type": 0
                        }
                    },
                    "detail": {
                        "Entries": [
                            {
                                "Value": "23505: duplicate key value violates ",
                                "Type": 0
                            }
                        ]
                    },
                    "source": {
                        "Value": “test Service",
                        "Type": 0
                    },
                    "incidentId": {},
                    "status": {
                        "Value": "500",
                        "Type": 1
                    }
                }
            ]
        }
    } }

實際上是使用Document.FromAttributeMap()來獲得上述結果,但我仍然需要讓它看起來像一個普通的 json 值,比如

 {
    "RecordDetails": {
        "firstName": “32432-ere”,
        "lastName": "32432-ere",
        "contactInformation": {
            "addressLine1": "10 Downing st",
            "addressLine2": “”,
            "city": "Portand",
            "phone": "555-555-5555",
            "email": "jdoe@abc.com”
        },
        "employer": {
            “Id”: “test:1473969"
        },
        "middleName": "A",
        "id": "637934995361318864",
        "title": "Dr.",
        "userId": “ererew-2332432”,
        "earnedDegrees": "MD.",        },
    "ValidationStatus":"Passed",
    "TypeOfRecord": “test”,
    "TenantCode": “demo2”,
    "Action":  "create",
    "TenantHostName": “t1.demo.com",
    "ImportJobId": “erewre7777-322h23h”,
    "ValidationMessage": "No Errors",
    "ProcessingResult": "Fail",
    "ProcessedAt": "2022-07-20T10:07:43.650Z",
    "RecordId":“dfdsf-3242-sdfds”,
    "ApiResponse": {
        "errors": {
            "Entries": [
                {
                    "code": "-1",
                    "internalErrorMessage": “”,
                    "links": {
                        "about": “”,
                        "help”:””                        },
                    "detail": {
                        "Entries": [
                            {
            "23505: duplicate key value violates "
                            }
                        ]
                    },
                    "source": “test Service",
                    "incidentId": {},
                    "status": “500”
                }
            ]
        }
    } }

任何人都可以通過建議一些 nuget 包或通過一些手動過程來幫助我實現這一點,嘗試了很多仍然無法實現這一點,抱歉,如果這是一個重復的問題。

提前致謝

您應該使用ToJsonPretty擴展方法從文檔對象中獲取漂亮的 JSON。

Document document = Document.FromAttributeMap(...); 
string jsonString = document.ToJsonPretty(); <--- This is what you need

對於此消息,您需要Amazon.DynamoDBv2.DocumentModel命名空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM