Back to .Net Jason Serializer
Copy your json string and it will give you the
classes.
and then use Newtonsoft.Json and it will give you a C# class
to work with.
//generated
with http://json2csharp.com/
public class User
{
public string country { get; set; }
public string email { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public object phone { get; set; }
public object zip { get; set; }
public string url { get; set; }
}
public class RootObject
{
public User user { get; set; }
}
and here is how we can de-serialize the json string:
var json = @"{'user': {
'country':'US',
'email':'testapi@example.com',
'first_name':'Test',
'last_name':'API',
'phone':null,
'zip':null,
'url':'https://new.site.com/xlogin/12325/abd9832cd92'
}
}";
RootObject userObj = JsonConvert.DeserializeObject<RootObject>(json.ToString());
However, you can take a little simple route as following here as well…
private void JsonDeserializeObject()
{
var json = @"{'user': {
'country':'US',
'email':'testapi@example.com',
'first_name':'Test',
'last_name':'API',
'phone':null,
'zip':null,
'loginurl':'https://new.site.com/xlogin/12325/abd9832cd92'
}
}";
var jsonSerializer = new JavaScriptSerializer();
var itemsList = (IDictionary<string, object>)jsonSerializer.DeserializeObject(json);
var user = (IDictionary<string, object>)itemsList["user"];
//getting the url
if (user != null)
{
var url = user["url"] as string;
}
}
And sending json via httppost is simple as well…
WebRequest httpWebRequest = null;
WebResponse
httpResponse = null;
try
{
httpWebRequest = (HttpWebRequest)WebRequest.Create(String.Format("{0}.json", URLTOPOSTTO));
httpWebRequest.ContentType = "application/json";//text/json
httpWebRequest.Method = "POST";
//authenticate if needed
if (isAuthenticate)
{
var authInfo = userName
+ ":" + password;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
httpWebRequest.Headers.Add("Authorization", "Basic " + authInfo);
}
//Pass api-key in the header, if needed
httpWebRequest.Headers.Add("API-key", apiKey);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var json = new JavaScriptSerializer().Serialize(new
{
first_name = person.FirstName,
last_name = person.LastName,
gender = person.Gender.ToString(),
email = person.Email,
zip = person.ZipCode,
country = "US",
phone = userPhone
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseFromServer = streamReader.ReadToEnd();
}
}
}
catch (WebException webEx)
{
}
catch (Exception ex)
{
}
finally
{
if (httpWebRequest != null)
httpWebRequest.GetRequestStream().Close();
if (httpResponse != null)
httpResponse.Close();
}
If the response we are getting back is JSON then we can quickly do the following to get an item out of it
if (!String.IsNullOrWhiteSpace(responseFromServer))
{
var jsonSerializer = new JavaScriptSerializer();
var itemsList = (IDictionary<string, object>) jsonSerializer.DeserializeObject(response);
var user = (IDictionary<string, object>) itemsList["user"];
if (user != null)
{
url = user["url"] as string;
}
}
{
var jsonSerializer = new JavaScriptSerializer();
var itemsList = (IDictionary<string, object>) jsonSerializer.DeserializeObject(response);
var user = (IDictionary<string, object>) itemsList["user"];
if (user != null)
{
url = user["url"] as string;
}
}