1

I have a JSON data (file attached). How can I parse it using C# using LitJSON? I am having trouble in deserialising it. Any help would be appreciated.

{
"vr-sessions" : {
"-KvDNAFD_BxlKEJ958eX" : {
  "interview" : "Android",
  "questions" : {
    "-KvG7BuWu4eI52pq-6uH" : {
      "playaudio" : "audio1",
      "question" : "Why cannot you run standard Java bytecode on Android?"
    },
    "-KvG9rxQv5DWJa1EMnhi" : {
      "playaudio" : "audio2",
      "question" : "Where will you declare your activity so the system can access it?"
    }
  }
},
"-KvDNOE8YhVdgovxrzrN" : {
  "interview" : "DevOps",
  "questions" : {
    "-KvMPd0v9BXnjYZxFm5Q" : {
      "playaudio" : "audio3",
      "question" : "Explain what is DevOps?"
    },
    "-KvMPi24OKeQTp8aJI0x" : {
      "playaudio" : "audio4",
      "question" : "What are the core operations of DevOps with application development and with infrastructure?"
    },
    "-KvMPqYxJunKp2ByLZKO" : {
      "playaudio" : "audio5",
      "question" : "Explain how “Infrastructure of code” is processed or executed in AWS?"
    }
  }
},
5
  • Your json doesn't seem to be valid. Provide a valid json. Commented Oct 15, 2017 at 14:33
  • @shaw it looks like he is only copying the first two objects of the json string. Commented Oct 15, 2017 at 15:26
  • 2
    Have you tried parsing it with LitJson? What happened? Please provide a minimal reproducible example showing how far you've got, and what happened. Commented Oct 15, 2017 at 15:49
  • @vipersassassin You are right. There are many objects like that. Could not copy all here. Sorry for not closing the JSON with brackets at the end. Commented Oct 16, 2017 at 8:43
  • 1
    @ManishKumar post a proper sample, code and an explanation of the problem. Don't force people to guess what the question is. "What's wrong" isn't a question. Also provide a link to LitJson. Google returns links to an abandoned Github project. Why don't you use an up-to-date deserializer like Json.NET? Commented Oct 16, 2017 at 10:51

1 Answer 1

2

After adding a couple of missing braces and removing last comma, your json seems to be valid. However there's no way to declare a class or member starting with dash in C#. Not sure how LitJson would be able to map json values to a C# class if names don't match, unless you replace dashes before parsing the string with LitJson.

Workaround

In your Solution Explorer right click References and do Add Reference, then from Assemblies->Framework select System.Web.Extensions.

string jsonString = File.ReadAllText("json.txt");
dynamic json = new JavaScriptSerializer().Deserialize<dynamic>(jsonString);

Navigate to the value you're looking for

string interview = json["vr-sessions"]["-KvDNAFD_BxlKEJ958eX"]["interview"];

Variable interview gets value "Android" which is the same as doing this:

var sessions = json["vr-sessions"];
string interview = sessions["-KvDNAFD_BxlKEJ958eX"]["interview"];

If you don't know session names

Iterate to get question and playaudio values.

foreach (var session in json["vr-sessions"].Values)
{
    foreach (var question in session["questions"].Values)
    {
        Console.WriteLine(question["question"]);
        Console.WriteLine(question["playaudio"]);
    }
}

Access to an element by position: let's say you want to iterate 0..N

var sessions = new List<dynamic>(json["vr-sessions"].Values);
Console.WriteLine(sessions[0]["interview"]);

This prints "Android" as the value of interview for session at position 0 is "Android".

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

2 Comments

Thanks for the code. But I don't know "-KvDNAFD_BxlKEJ958eX" in advance. And there are many keys like that, around 100. How can I access "playaudio" or "question" in this scenario?
If you don't know session's name the only way to go is using a dynamic object like the above. I have updated my answer to show you how to iterate and get those values, ignoring session names.

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.