0

I want to parse a json to List how can we do that. I have tried the following code but it didnt worked

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

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

And after getting it int list i want to loop through it

Here is my sample 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": []
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
vidyasagar85
  • 131
  • 1
  • 2
  • 11
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Jan 16 '15 at 04:26

4 Answers4

0

The best option could be to use the JsonConvert in order to parse Json into a List.

Reference: JSON Parsing in Windows Phone

Community
  • 1
  • 1
Kulasangar
  • 9,046
  • 5
  • 51
  • 82
0

You can use Json.Net.

To install Json.NET use NugetGallery : Json.net Nugets Gallery

And you can use json2Csharp.com for generate c# classes from json

Jerome
  • 63
  • 5
0

The JSON string you posted is not suitable for straight-forward deserialization to List. The easiest thing to do is use the online JSON 2 CSharp tool to generate classes and deserialize the json string to it. Here is an example of the generated classes:

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; }
}

And here is the logic for deserialization:

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

where the jsonStr variable holds the json string you posted.

Ilija Dimov
  • 5,221
  • 7
  • 35
  • 42
0

You need to use json2csharp tool to generate classes and deserialize the JSON string to list.

Here is the Classes generated from your JSON string.

 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; }
}

and Deserialize your JSON object using JsonConvert,

Suppose e.result is your JSON string then

 var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
 foreach (var blog in rootObject.Card)
        {
           //access your data like this- `blog.cardend;` or `blog.token;` 
        }
Charan Ghate
  • 1,384
  • 15
  • 32