0

i got this question i don't know is it possible or not. so i am getting this json data

{
  "data": {
    "student_score": {
      "time_elapsed": 10,
      "right_moves": 20,
      "wrong_moves": 10,
      "score": 166
    },
    "average_score": {
      "time_elapsed": 10,
      "right_moves": 20,
      "wrong_moves": 10,
      "score": 166
    },
    "total_student_count": 1
  },
  "status": "success",
  "time": 1379066567
}

i am able to coveting it to two class objects of these classes..

 public class StudentAssignementScoreClassForCompAssn : ViewModelBase
{
    private int _time_elapsed;
    private int _right_moves;
    private int _wrong_moves;
    private int _score;

    public int time_elasped
    {
        get
        {
            return _time_elapsed;
        }
        set
        {
            _time_elapsed = value;
            RaisePropertyChanged("time_elapsed");
        }
    }
    public int right_moves
    {
        get
        {
            return _right_moves;
        }
        set
        {
            _right_moves = value;
            RaisePropertyChanged("right_moves");
        }
    }
    public int wrong_moves
    {
        get
        {
            return _wrong_moves;
        }
        set
        {
            _wrong_moves = value;
            RaisePropertyChanged("wrong_moves");
        }
    }
    public int score
    {
        get
        {
            return _score;
        }
        set
        {
            _score = value;
            RaisePropertyChanged("score");
        }
    }
}

second class is same as it is..but with different name.. so my problem is this..the structure of the json data is variable like data fields may be more or less..so is there any way that i automatically create class of this data and can bind all the fields of this class(automatically :))..means data filling and binding with the view that does not depend on how the json coming( i will have separate file for the details of json format) ..i think you are getting my issue..any kind of help like idea or different way of working with json (anything) is appreciated..sorry for my bad english :(

loop
  • 9,002
  • 10
  • 40
  • 76

2 Answers2

1

Look at this. http://jsonclassgenerator.codeplex.com/SourceControl/latest

Also see this SO answer: Deserialize JSON into C# dynamic object?

There are so many results:

search?q=dynamically+create+c%23+class+from+json

Community
  • 1
  • 1
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
0

You need to create 2 more class like

public class YourClass
{
    public DataClass data {get;set;}
    public string status {get;set;}
    public long time {get;set;}
}

public class DataClass
{
    public StudentAssignementScoreClassForCompAssn  student_score {get;set;}
    public StudentAssignementScoreClassForCompAssn  average_score {get;set;}
    public int total_student_count {get;set;}
}

then use Newtonsoft.Json for conversion

JsonConvert.DeserializeObject<YourClass>(inputString)
Satpal
  • 132,252
  • 13
  • 159
  • 168