Droid_Dev
Droid_Dev

Reputation: 1162

Json Parsing using xamarin.forms

Hi i am very new to xamarin development, i want to parse a simple Json response and display the message which is coming from the server. The Json format is given below.

{"email": {"message": "Email already Verified"}}

any help?

Upvotes: 0

Views: 839

Answers (2)

Droid_Dev
Droid_Dev

Reputation: 1162

Thanks to all for the help provided, after some research i have created a class to save the values of the JSON and solved the issues,

the solution i am adding below

public class Email
{
    public string message { get; set; }
}

public class Success
{
    public Email email { get; set; }
}

Upvotes: 0

EvZ
EvZ

Reputation: 12169

There is an awesome service QuickType.io where you can just copy your JSON, select the target language and get a working example of deserialisation.

Here is the output generated for the JSON you shared above:

// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var welcome = Welcome.FromJson(jsonString);

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Welcome
    {
        [JsonProperty("email")]
        public Email Email { get; set; }
    }

    public partial class Email
    {
        [JsonProperty("message")]
        public string Message { get; set; }
    }

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

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

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}

Upvotes: 1

Related Questions