我有一个简单的json:
{
"timestamp": "38519277!12/14/2018 08:35:17",
"entity": "Account",
"entity": "Contact",
"entity": "Case"
}
我需要添加到词典中:
Dictionary<string, string> objects = new Dictionary<string, string>();
键是实体,值始终是相同的时间戳。我不确定如何进行。这是我以前从未做过的事情。有人可以建议吗?
答案 0 :(得分:0)
这里...
StringBuffer sb = new StringBuffer ();
sb.append ( QRCODE_IDENTIFICATIVO );
// other lines with the content of qrcode
QRCode qrCode = new QRCode ();
qrCode.setVersion ( versione );
qrCode.at ( modulesWidth, modulesHeight );
qrCode.setMatrixWidth ( modulesWidth );
qrCode.setECLevel ( ErrorCorrectionLevel.M );
Map<EncodeHintType, Object> qrParam = new HashMap<EncodeHintType, Object> ();
qrParam.put ( EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M );
qrParam.put ( EncodeHintType.CHARACTER_SET, "UTF-8" );
BarcodeQRCode qrcode = new BarcodeQRCode ( sb.toString (), (int) mmToPt ( 30f ), (int) mmToPt ( 30f ), qrParam );
return qrcode.createAwtImage ( Color.BLACK, Color.WHITE );
更多详细信息:
How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
答案 1 :(得分:0)
如果您想使用字典,唯一的方法就是将值部分作为对象类型或动态类型。
Dictionary<string, object> objects = new Dictionary<string, object>();
如果您要使用JsonConvert,则use this Example
答案 2 :(得分:0)
假设您有一个看起来像这样的类:
class Test
{
public string timestamp { get; set; }
public List<string> entity { get; set; }
}
...以及看起来像这样的json对象(由于注释中指出,您当前的json对象无效)
"{ \"timestamp\": \"38519277!12 / 14 / 2018 08:35:17\", \"entity\": [\"Account\", \"Contact\", \"Case\"] }"
反序列化var obj = JsonConvert.DeserializeObject<Test>(json);
这是您想要的代码行:
Dictionary<string, string> objects = obj.entity.ToDictionary(x => x, x => obj.timestamp);