2

I have a string array of the form

"education_history":
     [{"name":"xxxxxx",
       "concentrations":[],
       "school_type":"College"},
     {"name":"xxxxxxx",
      "concentrations":[],
      "school_type":"College"}]

I want to deserialize the values name and school_type. I have already deserialized the values of single value types but I am facing problems with string arrays.One more problem is that in my request I have multiple arrays which I want to deserealize.

Thanks for the help

svick
  • 236,525
  • 50
  • 385
  • 514
Aviral Kumar
  • 814
  • 1
  • 15
  • 40
  • 1
    This looks like JSON (except for the outer scope, which could be made into valid JSON simply by wrapping in `{…}`). Are you using a JSON parser? If not, you should. – Marcelo Cantos Feb 18 '12 at 11:10

4 Answers4

1

Like marcelo said, using a json parser is the way to go: http://json.codeplex.com/

aL3891
  • 6,205
  • 3
  • 33
  • 37
1

You have to use diffrent type instead of string.

Byte array with fixed size or StringBuilder type should work.

String is variable-lenght, that is the reason of problems in deserialization.

Kamil
  • 13,363
  • 24
  • 88
  • 183
1

I think my answer to Deserialization of a Facebook JSON string also applies for this question


Instead of declaring a lot of tiny classes I would go dynamic way. Here is the code for dynamic json object. And the usage would be something like this:

dynamic obj = JsonUtils.JsonObject.GetDynamicJsonObject(jsonString);

Console.WriteLine("{0} , {1}", obj.current_location.city, obj.current_location.state);

Console.WriteLine("EDUCATION");
foreach (var eduHist in obj.education_history)
{
    Console.WriteLine("{0} , {1}", eduHist.name, eduHist.year);
}

Console.WriteLine("WORK");
foreach (var workHist in obj.work_history)
{
    Console.WriteLine("{0}  {1}-{2}", workHist.company_name, workHist.start_date, workHist.end_date);
}
Community
  • 1
  • 1
L.B
  • 114,136
  • 19
  • 178
  • 224
1

Let's say you've below class :

public class EducationHistory
{
  public string name { get; set; }
  public string[] concentrations { get; set; }
  public string school_type { get; set; }
}

As already suggested by @aL3891, download Json.Net

And do like below :

EducationHistory objVal = JsonConvert.DeserializeObject<EducationHistory>(yourJsonString);

You can implement Ienumerable on above class to iterate through collection. Check out a question here, how it deserialize using Json.NET. I hope it helps!

Community
  • 1
  • 1
Nilesh Thakkar
  • 2,877
  • 1
  • 24
  • 43
  • dude if I have multiple values in a json string...then??I was able to deserealize a value corressponding to a single person, but what if I have 500 person education history in a string?? – Aviral Kumar Feb 18 '12 at 17:49
  • In that case you might want to create class like below : public class Data { public EducationHistory[] education_history { get; set; } //Other Properties } and you would now deserialize your json string to Data class. I hope it helps! – Nilesh Thakkar Feb 18 '12 at 17:56
  • Are you sure you did exactly that way? Are you getting any error? – Nilesh Thakkar Feb 18 '12 at 18:07
  • http://stackoverflow.com/questions/9341948/deserialization-of-a-facebook-json-string – Aviral Kumar Feb 18 '12 at 18:16
  • Would you mind converting public EducationHistory[] education_history { get; set; } to public List education_history { get; set; } do the same for likewise properties in your class. – Nilesh Thakkar Feb 18 '12 at 18:59