1

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

6
  • You missed generic argument in Deserialize function. Show us the class description you are trying to deserialize into. Commented Mar 14, 2015 at 15:55
  • i dint made one, i need one if i only need to get one arttribut? Commented Mar 14, 2015 at 16:01
  • Code you showed will not compile. Commented Mar 14, 2015 at 16:06
  • it compiles but it throws a exception (ît dont gets an object) Commented Mar 14, 2015 at 16:08
  • You are lying , you are using Deserialize<T>(String) signature without generic argument, Code is not compileable. Commented Mar 14, 2015 at 16:09

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.