E
E
eliasum2017-09-18 12:24:45
JSON
eliasum, 2017-09-18 12:24:45

Deserializing json in C# using Newtonsoft.Json.dll?

Hello. There is a response from the server in json format like this:
{
"user":{"ip":"192.168.216.11","login":"andi2222","number":12345},
"items":{
"1": {"name":"aaa","number":"","producer":"","measure":"24","suodID":"","foxproID":""},
"2": {"name":"bbb","number":"","producer":"","measure":"24","suodID":"","foxproID":""},
"3": {"name":"ccc","number":"","producer":"","measure":
// user dictionary /// /// json transformation class for getting data user /// public class userClass { public string ip { get; set; } public string login { get; set; } public int number { get; set; }
}
// parse the received json with information about users:
userArray = JsonConvert.DeserializeObject>(iJson);
How can I deserialize this json?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Chernyakov, 2017-09-18
@eliasum

Describe some Answer class (arbitrary name) that will store properties with the same names and types as in your json, and when deserializing, refer to this class like this:
Answer answer= JsonConvert.DeserializeObject< Answer >(iJson);
To quickly convert json data to c# classes, you can use this online service:
json2csharp.com

Descriptions of json classes from the service above
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var data = GettingStarted.FromJson(jsonString);
//
// For POCOs visit quicktype.io?poco
//
namespace QuickType
{
    using System;
    using System.Net;
    using System.Collections.Generic;

    using Newtonsoft.Json;

    public partial class GettingStarted
    {
        [JsonProperty("measures")]
        public Measures Measures { get; set; }

        [JsonProperty("number2")]
        public Number2 Number2 { get; set; }

        [JsonProperty("items")]
        public Items Items { get; set; }

        [JsonProperty("number1")]
        public Number1 Number1 { get; set; }

        [JsonProperty("user")]
        public User User { get; set; }
    }

    public partial class Measures
    {
        [JsonProperty("2")]
        public OtherThe1 The2 { get; set; }

        [JsonProperty("1")]
        public OtherThe1 The1 { get; set; }

        [JsonProperty("3")]
        public OtherThe1 The3 { get; set; }
    }

    public partial class OtherThe1
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }
    }

    public partial class Number2
    {
        [JsonProperty("2")]
        public OtherOtherOtherThe1 The2 { get; set; }

        [JsonProperty("1")]
        public OtherOtherOtherThe1 The1 { get; set; }

        [JsonProperty("3")]
        public OtherOtherOtherThe1 The3 { get; set; }
    }

    public partial class OtherOtherOtherThe1
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("parent")]
        public string Parent { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }
    }

    public partial class Items
    {
        [JsonProperty("2")]
        public The1 The2 { get; set; }

        [JsonProperty("1")]
        public The1 The1 { get; set; }

        [JsonProperty("3")]
        public The1 The3 { get; set; }
    }

    public partial class The1
    {
        [JsonProperty("measure")]
        public string Measure { get; set; }

        [JsonProperty("number")]
        public string Number { get; set; }

        [JsonProperty("foxproID")]
        public string FoxproID { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("producer")]
        public string Producer { get; set; }

        [JsonProperty("suodID")]
        public string SuodID { get; set; }
    }

    public partial class Number1
    {
        [JsonProperty("2")]
        public OtherOtherThe1 The2 { get; set; }

        [JsonProperty("1")]
        public OtherOtherThe1 The1 { get; set; }

        [JsonProperty("3")]
        public OtherOtherThe1 The3 { get; set; }
    }

    public partial class OtherOtherThe1
    {
        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }
    }

    public partial class User
    {
        [JsonProperty("login")]
        public string Login { get; set; }

        [JsonProperty("ip")]
        public string Ip { get; set; }

        [JsonProperty("number")]
        public long Number { get; set; }
    }


    public partial class GettingStarted
    {
        public static GettingStarted FromJson(string json) => JsonConvert.DeserializeObject<GettingStarted>(json, Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this GettingStarted self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }

    public class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
        };
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question