简体   繁体   中英

c# json parsing format

In c#, I'm trying to parse JSON that is in the following format. I've only been able to come close using the example code below, but it is very unstable.

(I'm also not sure how to parse in Javascript, which I also need to do.)

JSON example:

{"72": { "Rejected": true }, "271": { "PreApproved": true}}

Code example:

List<SSKChanges> lstSSK = new List<SSKChanges>();
string sskSource = "";
string sskStatus = "";
bool sskStatusBool = false;
int i = 0;
int iList = 0;
JsonTextReader reader = new JsonTextReader(new StringReader(jsonExample));
while (reader.Read())
{
    if (reader.Value != null)
    {
        if (i == 0)
        {
            int n;
            bool isNumeric = int.TryParse(reader.Value.ToString(), out n);
            if (isNumeric)
            {
                sskSource = reader.Value.ToString();
                i = 1;
            }
            else
            {
                sskStatus = reader.Value.ToString();
                i = 2;
            }
        }
        else if (i == 1)
        {
            sskStatus = reader.Value.ToString();
            i = 2;
        }
        else
        {
            sskStatusBool = (bool)reader.Value;
            i = 0;
            sskSource = "";
            sskStatus = "";
            sskStatusBool = false;
        }
    }
}

Assuming you are already using (as is suggested by your use of JsonTextReader ), you can load your JSON into a JObject then query the resulting object with LINQ to JSON .

For instance, given the class:

public class SSKChanges
{
    public string SskSource { get; set; }
    public string SskStatus { get; set; }
    public bool? SskStatusBool { get; set; }
}

You can do:

        var obj = JObject.Parse(jsonExample);
        var lstSSK = (from property in obj.Properties()
                      select new SSKChanges
                      {
                          SskSource = property.Name,
                          SskStatus = property.Value.Children<JProperty>().Select(p2 => p2.Name).FirstOrDefault(),
                          SskStatusBool = property.Value.Children<JProperty>().Select(p2 => (bool?)p2.Value).FirstOrDefault()
                      }).ToList();

As for the second question in your question, i'm also not sure how to parse in javascript, I need to do that too , you should ask a second question directed specifically at javascript experts.

Since you are already using Json.net, I would do it as:

string json = @"{""72"": { ""Rejected"": true }, ""271"": { ""PreApproved"": true}}";
var jobj = JObject.Parse(json);

foreach (var entry in jobj.Children().Cast<JProperty>())
{
    var kv = entry.Value.First() as JProperty;
    Console.WriteLine(entry.Name + "=>" + kv.Name + ":"  + kv.Value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM