Diom
Diom

Reputation: 121

Getting specific value from specific key of Document inside document

I have a document something like this

{ "_id": { "$oid" : "51776bca40bcc60038000001" }, 
"username": "domi55", 
"Password": "test", 
"Character": { "Job": "Warrior", 
               "Level": 1,
               "Skill": { "SkillID": "1001", 
                          "SkillName": "Blade Dance",
                          "LevelRequirment": 1 
                        }
              }
          }
}

How do I get the "Job" value and the "SkillName" value in C#? I'm using MongoDB and MongoDB C# Driver

Upvotes: 0

Views: 117

Answers (1)

I4V
I4V

Reputation: 35363

Using Json.Net

dynamic obj = JsonConvert.DeserializeObject(yourDoc);
Console.WriteLine("{0} {1}", obj.Character.Job, obj.Character.Skill.SkillName); 

or using JavaScriptSerializer

var obj = new JavaScriptSerializer().Deserialize<dynamic>(json);
Console.WriteLine("{0} {1}",obj["Character"]["Job"],obj["Character"]["Skill"]["SkillName"]); 

Upvotes: 1

Related Questions