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

1
  • 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. Commented Feb 18, 2012 at 11:10

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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!

5 Comments

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??
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!
Are you sure you did exactly that way? Are you getting any error?
Would you mind converting public EducationHistory[] education_history { get; set; } to public List<EducationHistory> education_history { get; set; } do the same for likewise properties in your class.
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);
}

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.