C#构造函数对象引用不设置对象的实例 [英] C# constructor Object reference not to set an instance of an object

查看:60
本文介绍了C#构造函数对象引用不设置对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使用web api来返回自定义json响应键。为此,我返回一个自定义类来返回json响应



自定义类

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

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控制器操作方法获取所有人员



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);
}





任何人都可以提供帮助,我需要这种格式的输出



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


-Thanks
Sindhu

推荐答案

这篇关于C#构造函数对象引用不设置对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆