1

I have a json object and I am trying to convert it to my c# object. Here is my JSON:

{"GuvenlikNoktaArray": {"GuvenlikNoktası": [{"Id": 1,"GuvenlikNoktası1":"SANTIYE","KartNo":"000001889174217","Sira": 1},{"Id": 2,"GuvenlikNoktası1":"INSAAT","KartNo":"000000803567858","Sira": 2},{"Id": 3,"GuvenlikNoktası1":"ÇALISMA","KartNo":"000003417926233","Sira": 3},{"Id": 4,"GuvenlikNoktası1":"GÜVENLIK","KartNo":"000001888909897","Sira": 4}]}}

And my c# class:

 public partial class GuvenlikNoktası
{
    public GuvenlikNoktası()
    {
        this.GüvenlikNoktasıOlay = new HashSet<GüvenlikNoktasıOlay>();
        this.PanikButonuAlarmlari = new HashSet<PanikButonuAlarmlari>();
    }

    public int Id { get; set; }
    public string GuvenlikNoktası1 { get; set; }
    public string KartNo { get; set; }
    public string Sira { get; set; }

    public virtual ICollection<GüvenlikNoktasıOlay> GüvenlikNoktasıOlay { get; set; }
    public virtual ICollection<PanikButonuAlarmlari> PanikButonuAlarmlari { get; set; }
}

And last, my convert try:

public void AddIstasyon(string json_string)
{
    GuvenlikNoktası result = new JavaScriptSerializer().Deserialize<GuvenlikNoktası>(json_string);
}

I don't get any errors but when I debuged, I see that all attributes inside 'result' are null. It seems like an empty object. How can I get a correct 'GuvenlikNoktası' object ? (Btw I am pretty sure I am getting the json object correctly).

4
  • 1
    First of all, your Sira property is defined as a string when in the JSON array, it is an integer. Commented Oct 4, 2016 at 8:37
  • 1
    More guide to use json with C#: newtonsoft.com/json Commented Oct 4, 2016 at 8:38
  • Secondly, when you're deserializing, it seems you're deserializing your JSON into a single instance of your GuvenlikNoktası class, when the JSON itself is an array of said class. Commented Oct 4, 2016 at 8:40
  • Your JSON and Class structure does not matchs. Also using Newtonsoft.Json gives much more performance. Commented Oct 4, 2016 at 8:47

3 Answers 3

2

If you must keep this JSON structure as-is you may use JObject to navigate inside your JSON properties until you reach your target objects to deserizlize. Please can you try the code below;

PS: This code uses Newtonsoft.Json;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SO_39847703
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = "{\"GuvenlikNoktaArray\": {\"GuvenlikNoktası\": [{\"Id\": 1,\"GuvenlikNoktası1\":\"SANTIYE\",\"KartNo\":\"000001889174217\",\"Sira\": 1},{\"Id\": 2,\"GuvenlikNoktası1\":\"INSAAT\",\"KartNo\":\"000000803567858\",\"Sira\": 2},{\"Id\": 3,\"GuvenlikNoktası1\":\"ÇALISMA\",\"KartNo\":\"000003417926233\",\"Sira\": 3},{\"Id\": 4,\"GuvenlikNoktası1\":\"GÜVENLIK\",\"KartNo\":\"000001888909897\",\"Sira\": 4}]}}";
            AddIstasyon(json);
        }

        public static void AddIstasyon(string json_string)
        {
            dynamic jsonObject = JObject.Parse(json_string);
            string jsonToDeserializeStrongType = jsonObject["GuvenlikNoktaArray"]["GuvenlikNoktası"].ToString();
            List<GuvenlikNoktası> result = JsonConvert.DeserializeObject<List<GuvenlikNoktası>>(jsonToDeserializeStrongType); ;
        }
    }

    public partial class GuvenlikNoktası
    {
        public GuvenlikNoktası()
        {
            this.GüvenlikNoktasıOlay = new HashSet<GüvenlikNoktasıOlay>();
            this.PanikButonuAlarmlari = new HashSet<PanikButonuAlarmlari>();
        }

        public int Id { get; set; }
        public string GuvenlikNoktası1 { get; set; }
        public string KartNo { get; set; }
        public string Sira { get; set; }

        public virtual ICollection<GüvenlikNoktasıOlay> GüvenlikNoktasıOlay { get; set; }
        public virtual ICollection<PanikButonuAlarmlari> PanikButonuAlarmlari { get; set; }
    }

    public class GüvenlikNoktasıOlay
    {

    }

    public class PanikButonuAlarmlari
    {

    }
}

Hope this helps

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

Comments

1

Your JSON data and your class definition do not fit together. Therefore the default values (NULL) are provided by the serializer.

In order to deserialize the given JSON data you need a class structure like:

public class Root
{
     public LevelOne GuvenlikNoktaArray {get; set;}
}

public class LevelOne {
    public IEnumerable<GuvenlikNoktası> GuvenlikNoktası {get; set;}
}

1 Comment

Yes, rboe is correct. Use this https://jsonformatter.curiousconcept.com/ to see how the structure looks, and then use this http://json2csharp.com/ so see the structure it needs to be parsed to.
0

You can use this class.

public class GuvenlikNoktası
{
    public int Id { get; set; }
    public string GuvenlikNoktası1 { get; set; }
    public string KartNo { get; set; }
    public int Sira { get; set; }
}

public class GuvenlikNoktaArray
{
    public IList<GuvenlikNoktası> GuvenlikNoktası { get; set; }
}

public class Example
{
    public GuvenlikNoktaArray GuvenlikNoktaArray { get; set; }
}

You can use this link For your referencehttp://jsonutils.com/.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.