我有一個(gè)用例,我將一些 json 文件轉(zhuǎn)換為 Go 結(jié)構(gòu)。我有以下文檔結(jié)構(gòu){ "contentVersion": "1.0.0.0", "paths" : [ { "ParameterReference": "someValue", }, { "ParameterReference": "someOtherValue", } ], "parameters": { "label": { "value": "label" }, "domainName": { "value": "domain" }, "servicePackageLink": { "value": "bin\\test.cspkg" }, "pfxContent": { "value": "SampleCert.Content" }, "sampleCertThumbprint": {"value": "SampleCert.Thumbprint"}, "anotherSampleCertContent":{ "value" : "AnotherSampleCert.Content" }, "anotherSampleCertThumbprint": {"value": "AnotherSampleCert.Thumbprint"} }, "secrets" :[ { "TargetReference":"SERviceConfiGURatiONLiNK", "Replacements": { "__Secret_Sample__" : { "SecretId":"secretID", "EncryptWith" :"encryptWithValue" }, "__Another_Secret_Sample__" : { "SecretId":"anotherSecretValue", "EncryptWith" :"anotherEncryptWithValue" }, "__Storage_ConnectionString__" : "someConnectionString" }, }, { "TargetReference":"None", "Certificates":[ { "Name":"AnotherSampleCert", "ContentReference" : "contentReference" } ] } ]} 我面臨的問(wèn)題是 json 沒(méi)有固定的模式,例如。如果您檢查parameters對(duì)象,特別是名稱(chēng)為 的條目sampleCertSchema,在我的用例中,用戶(hù)可以提供從 0 到 N 的任意數(shù)量的此類(lèi)____CertSchemas值。因此,參數(shù)字段也可以如下"parameters": { "label": { "value": "label" }, "domainName": { "value": "domain" }, "servicePackageLink": { "value": "bin\\test.cspkg" }, "pfxContent": { "value": "SampleCert.Content" } },這將打印一個(gè)空結(jié)構(gòu)。我的問(wèn)題是,我應(yīng)該如何在 Go 中定義一個(gè)結(jié)構(gòu),以便它能夠加載一個(gè)帶有非預(yù)定義條目的 json。使用 java/C# 我可以使用Object該類(lèi)并創(chuàng)建一個(gè)文檔,但我無(wú)法理解使用 Go 來(lái)執(zhí)行此操作。
2 回答

千萬(wàn)里不及你
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
go 中的等價(jià)物是 interface{},即空接口。例如,查看這個(gè)答案:Unmarshaling Into an Interface{} and Then Performing Type Assertion
一旦您知道要搜索的術(shù)語(yǔ),您應(yīng)該能夠在需要時(shí)搜索更詳細(xì)的內(nèi)容。
- 2 回答
- 0 關(guān)注
- 166 瀏覽
添加回答
舉報(bào)
0/150
提交
取消