0

I want to serialize some json data I get from the web to classes and use the data, so I went to http://json2csharp.com/ and turned the json as below

json: [{"line_descr":"\u03a0\u0395\u0399\u03a1\u0391\u0399\u0391\u03a3 - 
\u0392\u039f\u03a5\u039b\u0391","line_descr_eng":"PEIRAIAS - VOYLA"}]

To this class:

    public class RootObject
    {
        public string line_descr { get; set; }
        public string line_descr_eng { get; set; }
    }

This is my code:

class LineName
{
    public async static Task<RootObject> GetLineName(int linecode)
    {
        var http = new HttpClient();
        var response = await http.GetAsync("http://telematics.oasa.gr/api/?act=getLineName&p1=962");
        var result = await response.Content.ReadAsStringAsync();

        var serializer = new DataContractJsonSerializer(typeof(RootObject));
        var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
        var data = (RootObject)serializer.ReadObject(ms);

        return data;
    }
}

[DataContract]
public class RootObject
{
    [DataMember]
    public string line_descr { get; set; }

    [DataMember]
    public string line_descr_eng { get; set; }
}


    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        RootObject myLine = await LineName.GetLineName(92);
        ResultTextBlock.Text = myLine.line_descr_eng;
    }

So when I try to get the data and display it in my textblock I get the error: line_descr_eng is null.

Can someone point where the fault is ? since the line_descr_eng should be PEIRAIAS - VOYLA but mine is null and after a lot of searching I cant find where the fault is.

lindexi
  • 4,182
  • 3
  • 19
  • 65
panoukos41
  • 41
  • 9

2 Answers2

2

Your json is an array, not an object, and you should deserialize it into an array.

public async static Task<RootObject[]> GetLineName(int linecode)
{
    var http = new HttpClient();
    var response = await http.GetAsync("http://telematics.oasa.gr/api/?act=getLineName&p1=962");
    var result = await response.Content.ReadAsStringAsync();

    var serializer = new DataContractJsonSerializer(typeof(RootObject[]));
    var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
    var data = (RootObject[])serializer.ReadObject(ms);

    return data;
}
//...


 var myLines = await LineName.GetLineName(92);
 var myLine = myLines.FirstOrDefault();

Also you don't need a memory stream, you can read stream from the http response

var result = await response.Content.ReadAsStreamAsync();
Oleh Nechytailo
  • 2,155
  • 17
  • 26
  • ok i tried this but it says it cant convert RootObject[] to RootObject the error is at the "return data". Can you specify it a bit more?? :) – panoukos41 Jun 24 '17 at 22:57
  • 1
    @PanosPanosAthanasiou edited the post. Basically you need to change return type of the method and type of the variable to match the new data type. – Oleh Nechytailo Jun 25 '17 at 09:18
  • ok it worked ^_^ thnx!! just the second var needs a different name from the first :) So basicly we return an array and then we just get the first item from that array right ? – panoukos41 Jun 25 '17 at 09:33
  • 1
    @PanosPanosAthanasiou yes, your json is an encoded array, this code decodes it as an array and gets the first element of it, or null if array is empty. – Oleh Nechytailo Jun 25 '17 at 12:37
  • ok thanks a lot again, after your answer i was able to sort out everything ^_^ !! – panoukos41 Jun 25 '17 at 14:25
0

You simple can use the JavaScriptSerializer class instead of DataContractJsonSerializer like this:

Replace:

var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (RootObject)serializer.ReadObject(ms);

with this:

var ser = new JavaScriptSerializer();
var test = ser.Deserialize<List<RootObject>>(json);

If you cannot find JavaScriptSerializer, then you have to do the simple following steps:

  1. Right click References and do Add Reference, then from Assemblies->Framework select System.Web.Extensions.
  2. Now you should be able to add the following to your class file:

    using System.Web.Script.Serialization;
    

Cited from: https://stackoverflow.com/a/15391388/5056173

Sean Stayns
  • 4,082
  • 5
  • 25
  • 35