Friday, March 28, 2014

C# constructor Object reference not to set an instance of an object

HI I am trying to use web api in order to return custom json response keys. For this i have return a custom class to return json response

Custom Class:



using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web;
using MvcApplication1.Models;
using Newtonsoft.Json.Serialization;

namespace MvcApplication1.Generic
{
public class JsonResponseHandler
{

public static Dictionary<string, List<Dictionary<string, string>>> rootDictionary;

public static JsonMediaTypeFormatter formatter;


static JsonResponseHandler()
{


// TODO: Complete member initialization

Dictionary<string, string> responseDictionary = new Dictionary<string, string>()
{
{ "status", "true"}, { "error_code", "200"}, { "message", "not found"}, { "request-_type", "login"}

};
//Response Array
List<Dictionary<string,string>> responseArray= new List<Dictionary<string,string>>();

responseArray.Add(responseDictionary);


//Data objects array
IList<Person> result = new List<Person>();
result.Add(new Person
{
Name = "Ugo",
DOB = DateTime.Now,
Address = "Lattanzi",
});
result.Add(new Person
{
Name = "adfasdf",
DOB = DateTime.Now,
Address = "asdfasdf",
});




List<Dictionary<string,string>> dataDictionariesArray= new List<Dictionary<string,string>>();

//Converting dataobjects to dictionary
foreach(Person person in result)
{

Dictionary<string,string> dataObjectsDictionary= new Dictionary<string,string>();

dataObjectsDictionary.Add("Name", person.Name);
dataObjectsDictionary.Add("DOB", person.DOB.ToString());
dataObjectsDictionary.Add("Address", person.Address);


dataDictionariesArray.Add(dataObjectsDictionary);
}


//Root dictionary
//Dictionary<string, List<Dictionary<string, string>>> rootDictionary = new Dictionary<string, List<Dictionary<string, string>>>();
rootDictionary.Add("response", responseArray);
rootDictionary.Add("data", dataDictionariesArray);



//Formatter

// JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
var json = formatter.SerializerSettings;
var format = new HttpResponseMessage(HttpStatusCode.OK);
json.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
json.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
json.Formatting = Newtonsoft.Json.Formatting.Indented;
json.ContractResolver = new CamelCasePropertyNamesContractResolver();
json.Culture = new CultureInfo("it-IT");
}

}
}

API Controller Action method for getting all the persons



public HttpResponseMessage Get()
{
Response response = new Response();
response.errorCode = 100;

return Request.CreateResponse(HttpStatusCode.OK, JsonResponseHandler.rootDictionary, JsonResponseHandler.formatter);

}

I need output in this json format:



{
"response": [
{
"status": "true",
"error_code": "200",
"message": "not found",
"request-_type": "login"
}
],
"data": [
{
"name": "Ugo",
"dob": "28-03-2014 PM 12:48:23",
"address": "Lattanzi"
},
{
"name": "adfasdf",
"dob": "28-03-2014 PM 12:48:23",
"address": "asdfasdf"
}
]
}


No comments:

Post a Comment