2

这是我的 json 字符串:

{"loginAccounts": [
    {
        "name": "abc",
        "accountId": "123456",
        "baseUrl": "https://abc.defghi.com/test/t12/remark/123456",
        "isDefault": "true",
        "userName": "Ceilina James",
        "userId": "7c5bat57-850a-5c93-39eb-2015ai9o8822",
        "email": "abc@test.com",
        "siteDescription": ""
    }
]}

我需要“baseUrl”值。如何在 C# 中找到它?

4

2 回答 2

6

您可以使用 JSON 序列化程序(例如JavaScriptSerializer该类)将此 JSON 字符串反序列化为 C# 类,然后提取所需的值。例如,您可能有以下模型:

public class SomeModel
{
    public LoginAccount[] LoginAccounts { get; set; }
}

public class LoginAccount
{
    public string Name { get; set; }
    public string AccountId { get; set; }
    public string BaseUrl { get; set; }
    public string IsDefault { get; set; }
    ...
}

接着:

string json = "... your JSON string comes here ...";
var serializer = new JavaScriptSerializer();
string json = ...
var model = (SomeModel)serializer.Deserialize(json);
foreach (var account in model.LoginAccounts)
{
    string baseUrl = account.BaseUrl;
    ...
}
于 2013-09-06T08:49:08.300 回答
2

使用Json.Net

foreach (var acc in JObject.Parse(json)["loginAccounts"])
{
    Console.WriteLine(acc["baseUrl"]);
}
于 2013-09-06T08:50:04.947 回答