Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize a JSON array in C#

I'm stuck with a tricky problem.

I've a JSON string of this format:

[{
  "record":
          {
             "Name": "Komal",
             "Age": 24,
             "Location": "Siliguri"
          }
 },
 {
  "record":
          {
             "Name": "Koena",
             "Age": 27,
             "Location": "Barasat"
          }
 },
 {
  "record":
          {
             "Name": "Kanan",
             "Age": 35,
             "Location": "Uttarpara"
          }
 }
... ...
]

Fields in "record" can increase or decrease.

So, I've made classes like this:

public class Person
{
    public string Name;
    public string Age;
}

public class PersonList
{
    public Person record;
}

And trying to deserialize like this:

JavaScriptSerializer ser = new JavaScriptSerializer();

var r = ser.Deserialize<PersonList>(jsonData);

I'm doing something wrong. But unable to find. Can you please help.

Thanks in advance.

Update:

Actually I was getting error "Invalid JSON Primitive: ." due to I was getting the string reading a file with this code:

public static bool ReadFromFile(string path, string fileName, out string readContent)
{
   bool status = true;

   byte[] readBuffer = null;
   try
   {
      // Combine the new file name with the path
      string filePath = System.IO.Path.Combine(path, fileName);
      readBuffer = System.IO.File.ReadAllBytes(filePath);
   }
   catch (Exception ex)
   {
       status = false;
   }

   readContent = (null != readBuffer) ? Utilities.GetString(readBuffer) : string.Empty;

   return status;
}

Now I'm reading the file with this:

using (StreamReader r = new StreamReader("E:\\Work\\Data.json"))
{
   string json = r.ReadToEnd();
   result = JsonConvert.DeserializeObject<List<PersonList>>(json);
}

It's working fine.

like image 821
Arnab Das Avatar asked May 31 '13 12:05

Arnab Das


People also ask

Can T Deserialize JSON array into?

bind. JsonbException: Can't deserialize JSON array into: class exception occurs when you try to convert json string data to a java object. If the json string contains an array of objects and attempts to deserialize the json string to a json object, the array of objects could not be assigned to a single java object.


3 Answers

This should work...

JavaScriptSerializer ser = new JavaScriptSerializer();
var records = new ser.Deserialize<List<Record>>(jsonData);

public class Person
{
    public string Name;
    public int Age;
    public string Location;
}
public class Record
{
    public Person record;
}
like image 137
I4V Avatar answered Oct 08 '22 15:10

I4V


This code is working fine for me,

var a = serializer.Deserialize<List<Entity>>(json);
like image 15
Hitesh Avatar answered Oct 08 '22 15:10

Hitesh


[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("Age")]
public int required { get; set; }
[JsonProperty("Location")]
public string type { get; set; }

and Remove a "{"..,

strFieldString = strFieldString.Remove(0, strFieldString.IndexOf('{'));

DeserializeObject..,

   optionsItem objActualField = JsonConvert.DeserializeObject<optionsItem(strFieldString);
like image 2
Mohan Kumar Avatar answered Oct 08 '22 15:10

Mohan Kumar