48

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?

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Arnab Das
  • 3,548
  • 8
  • 31
  • 43
  • 3
    How is .net supposed to know that you want a `Person` object for a javascript object named `"record"`? – Bazzz May 31 '13 at 12:08
  • 6
    If it's an option, I highly recommend using [JSON.NET](http://json.codeplex.com/). Then you could just call something like `JsonConvert.DeserializeObject>(jsonData)` – valverij May 31 '13 at 12:31
  • The error was: Invalid JSON primitive: . – Arnab Das Jun 01 '13 at 13:53
  • I had a similar issue till @valverij comment above fixed my issue. > – user2025696 Oct 28 '15 at 19:31
  • Note to future readers. ['Can somebody help me?' is not an actual question](https://meta.stackoverflow.com/a/284237/11107541). Please ask real questions! – starball Dec 21 '22 at 04:54

3 Answers3

53

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;
}
BenKoshy
  • 33,477
  • 14
  • 111
  • 80
I4V
  • 34,891
  • 6
  • 67
  • 79
15

This code is working fine for me,

var a = serializer.Deserialize<List<Entity>>(json);
Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
Hitesh
  • 3,508
  • 1
  • 18
  • 24
1
[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);
Alejandro
  • 7,290
  • 4
  • 34
  • 59
Mohan Kumar
  • 647
  • 7
  • 8