JSON:
{
"id": 1,
"name": "RobloHunks",
"description": "",
"owner": {
"buildersClubMembershipType": "None",
"userId": 1179762,
"username": "RobloTim",
"displayName": "RobloTim"
},
"shout": null,
"memberCount": 15915,
"isBuildersClubOnly": false,
"publicEntryAllowed": true
}
我要打印的内容:
RobloTim
嗯,“owner”目录中的“username”值。
有几种方法,其中一种涉及创建模型和反序列化。
但如果您只需要1个属性,则这将起作用:
dynamic obj = JObject.Parse(yourJson);
string userName = obj.owner.username
Console.WriteLine(userName);
您将需要newtonsoft.json包
PM>;安装-打包newtonsoft.json
将结果反序列化为以下对象,如下所示。。
https://json2csharp.com/
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Owner {
public string buildersClubMembershipType { get; set; }
public int userId { get; set; }
public string username { get; set; }
public string displayName { get; set; }
}
public class Root {
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public Owner owner { get; set; }
public object shout { get; set; }
public int memberCount { get; set; }
public bool isBuildersClubOnly { get; set; }
public bool publicEntryAllowed { get; set; }
}
我之前也做了同样的事情,我创建了一个类,然后用JSONConverter库把所有的转换成那个类,这里有json到类的转换器,我不想把任何东西商业化,但是它们很容易找到