Last active
August 29, 2015 14:13
-
-
Save ghuntley/8f6d39be91cc27580ec4 to your computer and use it in GitHub Desktop.
JSON response - L7, L14, L23 aka domain names are heterogeneous resulting in difficulty parsing the resulting json from strongly typed languages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
namespace Name | |
{ | |
public class CheckDomainAvailabilityDomains | |
{ | |
public bool avail { get; set; } | |
public bool backorder { get; set; } | |
public string domainname { get; set; } | |
public bool premium { get; set; } | |
public decimal? price { get; set; } | |
public string tld { get; set; } | |
} | |
public class CheckDomainAvailabilityJsonConverter : JsonConverter | |
{ | |
public override bool CanConvert(Type objectType) | |
{ | |
return objectType == typeof (CheckDomainAvailabilityDomains); | |
} | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, | |
JsonSerializer serializer) | |
{ | |
JObject jsonObject = JObject.Load(reader); | |
JToken resultToken = jsonObject["result"]; | |
var result = JsonConvert.DeserializeObject<Result>(resultToken.ToString()); | |
var domains = new List<CheckDomainAvailabilityDomains>(); | |
List<JToken> domainTokens = jsonObject["domains"].Children().ToList(); | |
foreach (JProperty domainToken in domainTokens) | |
{ | |
var domain = JsonConvert.DeserializeObject<CheckDomainAvailabilityDomains>(domainToken.Value.ToString()); | |
domain.domainname = domainToken.Name; | |
domains.Add(domain); | |
} | |
//var suggested = new List<CheckDomainAvailabilityDomains>(); | |
//List<JToken> suggestedTokens = jsonObject["suggested"].Children().ToList(); | |
//foreach (JProperty suggestedToken in suggestedTokens) | |
//{ | |
// var suggestion = | |
// JsonConvert.DeserializeObject<CheckDomainAvailabilityDomains>(suggestedToken.Value.ToString()); | |
// suggestion.domainname = suggestedToken.Name; | |
// suggested.Add(suggestion); | |
//} | |
return new CheckDomainAvailabilityResponse() | |
{ | |
Result = result, | |
domains = domains, | |
//suggested = suggested | |
}; | |
} | |
public override | |
void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
{ | |
throw | |
new | |
NotImplementedException(); | |
} | |
} | |
[JsonConverter(typeof (CheckDomainAvailabilityJsonConverter))] | |
public class CheckDomainAvailabilityResponse : ResponseBase | |
{ | |
public List<CheckDomainAvailabilityDomains> domains { get; set; } | |
public List<CheckDomainAvailabilityDomains> suggested { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"result" : { | |
"code" : 100, | |
"message" : "Command Successful" | |
}, | |
"domains" : { | |
"testdomain1.com" : { | |
"avail" : true, | |
"tld" : "com", | |
"price" : "9.99", | |
"premium" : false, | |
"backorder" : false | |
}, | |
"testdomain1.net" : { | |
"avail" : false, | |
"tld" : "net", | |
"price" : "49.95", | |
"premium" : false, | |
"backorder" : true | |
} | |
}, | |
"suggested" : { | |
"largecat.net" : { | |
"avail" : true, | |
"tld" : "net", | |
"price" : "9.99", | |
"premium" : false, | |
"backorder" : false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment