Json deserialization with Datacontractjsonserializer Null Pointer Exception

Asked

Viewed 97 times

1

At first I’m doing a test if I can communicate properly with the Wikipedia API. My first goal is to take data from the API, turn it into an object and present something on the screen that demonstrates that this process is correct.

I created a package called Wikipediaviewer and put in it a class called Wikipediamapproxy, which has a method whose goal is to make the request, take the json and turn it into a c#object. Also, within the Wikipediaviewer package I put several classes that will serve to be used in the creation of the object, resulting from json.

My doubts:

1) On my Mainpage.xaml.Cs, is giving a null Pointer on line 33:

https://github.com/raulfmiranda/csharp/blob/master/WikipediaViewer/WikipediaViewer/MainPage.xaml.cs

This means that some problem is occurring when taking json and turning it to object. In Wikipediamapproxy, I’m using the Datacontractjsonserializer class that does the job of transforming json into the C#object. So I put [Datacontract] on top of each class and [Datamember] on top of each property, to inform Datacontractjsonserialize how it should do this transformation. However, the attribute names are not exactly as in json. For example, in json, there is no Item, because there they are named by a numbering, but I decided to call it an item. Also, the Pages class was to contain multiple items, so I put a list of items inside. I don’t know if this is correct.

2) There is a problem on line 23: https://github.com/raulfmiranda/csharp/blob/master/WikipediaViewer/WikipediaViewer/WikipediaMapProxy.cs#L23

Encoding.UTF8.Getbytes() takes a string as parameter, but if I just put result as parameter, the compiler informs that it cannot make an implicit conversion from a Task to a string, so I decided to put as result parameter. Result, but I don’t know if this is right. What do you think?

See the Code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace WikipediaViewer
{
    public class WikipediaMapProxy
    {
        public async static Task<RootObject> GetResult(string searchedString)
        {
            string jsonSource = "https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=2&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=gandhi&origin=*";
            var http = new HttpClient();
            var response = await http.GetAsync(jsonSource);
            var result = response.Content.ReadAsStringAsync();
            var serializer = new DataContractJsonSerializer(typeof(RootObject));

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

            return data;
        }
    }

    [DataContract]
    public class Thumbnail
    {
        [DataMember]
        public string source { get; set; }
        [DataMember]
        public int width { get; set; }
        [DataMember]
        public int height { get; set; }
    }

    [DataContract]
    public class Item
    {
        [DataMember]
        public int pageid { get; set; }
        [DataMember]
        public int ns { get; set; }
        [DataMember]
        public string title { get; set; }
        [DataMember]
        public int index { get; set; }
        [DataMember]
        public Thumbnail thumbnail { get; set; }
        [DataMember]
        public string pageimage { get; set; }
        [DataMember]
        public string extract { get; set; }
    }

    [DataContract]
    public class Pages
    {
        [DataMember]
        public List<Item>  item { get; set; }
    }

    [DataContract]
    public class Query
    {
        [DataMember]
        public Pages pages { get; set; }
    }

    [DataContract]
    public class RootObject
    {
        [DataMember]
        public Query query { get; set; }
    }
}
  • I must remember reading this post: https://answall.com/questions/706/como-converter-uma-resposta-em-json-para-um-objeto-em-c?rq=1

  • I think I found the answer here: https://stackoverflow.com/questions/41555627/what-is-the-most-effective-way-to-parse-json-objects-that-are-dynamically-named/41555809#41555809

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.