Click here to Skip to main content
16,019,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI I am trying to use web api inorder to return custom json response keys. For this i have return a custom class to return json response

Custom Class
C#
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
C#
public HttpResponseMessage Get()
{
    Response response = new Response();
    response.errorCode = 100;
    return Request.CreateResponse(HttpStatusCode.OK, JsonResponseHandler.rootDictionary, JsonResponseHandler.formatter);
}


Can anyone help regarding this i need output in this 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"
    }
  ]
}

-Thanks
Sindhu
Posted
Updated 27-Mar-14 23:00pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900