我将cookie导出到Json文件中.看起来是这样的:
[
{
"name": "abc",
"value": "0000",
"domain": ".domain.com",
"hostOnly": false,
"path": "/",
"secure": false,
"httpOnly": false,
"sameSite": "no_restriction",
"session": false,
"firstPartyDomain": "",
"partitionKey": null,
"expirationDate": 1734526188,
"storeId": null
},
{
"name": "dfg",
"value": "777",
"domain": ".domain.it",
"hostOnly": false,
"path": "/",
"secure": true,
"httpOnly": true,
"sameSite": "no_restriction",
"session": false,
"firstPartyDomain": "",
"partitionKey": null,
"expirationDate": 1684947948,
"storeId": null
}
]
如您所见,有些值是strings
,例如name、hostOnly是boolean
、partitionKey是nullable
、expirationDate是integer
.
这是我使用的C代码
json = File.ReadAllText("json.txt");
JArray array = JArray.Parse(json);
foreach(JObject cookie_json in array)
{
foreach(var entry in cookie_json)
{
cookies.Add(new Cookie(entry.Key, (string?)entry.Value));
}
}
但在这里,我发现了几个错误:
-
Cookie()
构造函数只接受字符串作为Value
,所以我不能添加整数、布尔值和不可为null的元素,因为可为null的元素会引发异常 - 我正在 for each 条目(行)而不是每个Json元素(代表一个Cookie)创建一个新Cookie.我怎样才能解决这个问题?