0

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;
        }
    }
}
2
  • I assume you're using Json.Net. I think you should be looking at the DeserializeObject method. Also, if you are using Web API or even MVC methods there are other ways of doing this too. Since you didn't specify where you are getting your JSON, I'm assuming it is coming from a file and so you need to deserialize it manually. Commented Feb 29, 2016 at 20:34
  • Please be sure to include what your expected output is. Refer to this for more: stackoverflow.com/help/mcve Commented Mar 2, 2016 at 22:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.