Reputation: 35
im currently making my first steep with json and well im complety confused. I found many examples how to deserialize json files but nothing helps me.
{
"102": {
"id": 102,
"name": "cvmember3",
"profileIconId": 28,
"revisionDate": 1373593599000,
"summonerLevel": 1
},
"101": {
"id": 101,
"name": "IS1ec76a704e9b52",
"profileIconId": -1,
"revisionDate": 1355466175000,
"summonerLevel": 1
}
}
This is the json i object i got, the problem is im to stupid to deseralize it. What i tried till now:
String name= (string) new JavaScriptSerializer().Deserialize<Dictionary<String, object>>(json)["name"];
Im missing the index, and dont know how to add it anyone, can say me the correct line to deserialize, i has acces to the libary json.net
Upvotes: 1
Views: 699
Reputation: 772
Alternatively you could define a class for the data you're going to get back and then parse your JSON into a dictionary something like this:
public class DataClass
{
public int id;
public string name;
public int profileIconId;
public long revisionDate;
public int summonerLevel;
}
Then
Dictionary<int, DataClass> myDictionary = JsonConvert.DeserializeObject<Dictionary<int, DataClass>>(json);
string foundName = myDictionary[102].name;
Upvotes: 1
Reputation: 38865
If you only want one item from the string (i only need to get one arttribut
), you can use NewtonSoft to fish out the item you need:
using Newtonsoft.Json.Linq;
// read from where ever
string jstr = File.ReadAllText("C:\\Temp\\101.json");
JObject js = JObject.Parse(jstr);
var data102 = js["102"]["name"]; // == "cvmember3"
var data101 = js["101"]["name"]; // == "IS1ec76a704e9b52"
Console.WriteLine("Name for 101 is '{0}'", data101.ToString());
Console.WriteLine("Name for 102 is '{0}'", data102.ToString());
Output:
Name for 101 is 'IS1ec76a704e9b52'
Name for 102 is 'cvmember3'
This is a quick way to get at just one item value but it assumes you know what it looks like and where it is stored.
Upvotes: 0