将Json解析为List

时间:2015-01-15 14:32:36

标签: json windows-phone-8 json.net

我想解析一个json到List我们如何做到这一点。我尝试了以下代码,但它没有用

Dictionary<string, object> pGateways=(Dictionary<string,object>)Json.JsonParser.FromJson(jsonString);

List<object> creditOptions = new List<object>();
creditOptions = (List<object>)pGateways;

在获得它的int列表之后我想循环它

这是我的样本json

{
    "MessageCode": "CS2009",
    "Status": "Y",
    "ErrorCode": "0",
    "ErrorDescription": "Success",
    "account": 
     {
        "card": 
        [
            {
                "cardend": "asd",
                "token": "aads",
                "cardstart": "asdad",
                "accounttype": "asda",
                "cardnetwork": "as",
                "issuer": "asd",
                "customername": "a",
                "expdate": "04/2018"
            },
            {
                "cardend": "asda",
                "token":"adssadsa",
                "cardstart": "asd",
                "accounttype": "asd",
                "cardnetwork": "asd",
                "issuer": "asda",
                "customername": "asd",
                "expdate": "03/2016"
            }
        ],
        "bank": []
    }
}

4 个答案:

答案 0 :(得分:0)

最好的选择可能是使用JsonConvert来将Json解析为List

参考: JSON Parsing in Windows Phone

答案 1 :(得分:0)

您可以使用Json.Net。

要安装Json.NET,请使用NugetGallery:Json.net Nugets Gallery

您可以使用json2Csharp.com从json

生成c#类

答案 2 :(得分:0)

您发布的JSON字符串不适合直接反序列化到List。最简单的方法是使用在线 JSON 2 CSharp 工具生成类并将json字符串反序列化。以下是生成的类的示例:

public class Card
{
    public string cardend { get; set; }
    public string token { get; set; }
    public string cardstart { get; set; }
    public string accounttype { get; set; }
    public string cardnetwork { get; set; }
    public string issuer { get; set; }
    public string customername { get; set; }
    public string expdate { get; set; }
}

public class Account
{
    public List<Card> card { get; set; }
    public List<object> bank { get; set; }
}

public class RootObject
{
    public string MessageCode { get; set; }
    public string Status { get; set; }
    public string ErrorCode { get; set; }
    public string ErrorDescription { get; set; }
    public Account account { get; set; }
}

以下是反序列化的逻辑:

var root = JsonConvert.DeserializeObject<RootObject>(jsonStr);

其中jsonStr变量包含您发布的json字符串。

答案 3 :(得分:0)

您需要使用json2csharp工具生成类并反序列化要列出的JSON字符串。

这是从您的JSON字符串生成的类。

 public class Card
{
    public string cardend { get; set; }
    public string token { get; set; }
    public string cardstart { get; set; }
    public string accounttype { get; set; }
    public string cardnetwork { get; set; }
    public string issuer { get; set; }
    public string customername { get; set; }
    public string expdate { get; set; }
}

public class Account
{
    public List<Card> card { get; set; }
    public List<object> bank { get; set; }
}

public class RootObject
{
    public string MessageCode { get; set; }
    public string Status { get; set; }
    public string ErrorCode { get; set; }
    public string ErrorDescription { get; set; }
    public Account account { get; set; }
}

使用JsonConvert

反序列化JSON对象

假设e.result是你的JSON字符串,那么

 var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
 foreach (var blog in rootObject.Card)
        {
           //access your data like this- `blog.cardend;` or `blog.token;` 
        }