【问题标题】:Can we parse JSON data before returning to Client using ASP.NET WEB API我们可以在使用 ASP.NET WEB API 返回客户端之前解析 JSON 数据吗
【发布时间】:2015-01-03 21:23:13
【问题描述】:

我希望我的 Json 数据如下所示:

 {"Person": {
    "PersonId": "1"
    "Name":"Brad",
    "Age":"45",
   ...
  }

其中“Person”是返回的对象类型。

当我看到 XML 格式时,它总是返回指定对象类型的数据

<Person>
     <PersonId>1</PersonId>
     <Name>Brad</Name>
     <Age>45</Age>
</Person>
 Where Person is type of Object.

那么我们有什么方法可以得到上述格式中指定的 Json 数据吗?

【问题讨论】:

    标签: json asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    可能是:

    public class Person
    {
        public int PersonId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
    
    public class PersonController : ApiController
    {
        /*{"ArrayOfPerson":[{"PersonId":1,"Name":"Brad","Age":45},{"PersonId":2,"Name":"Mary","Age":30}]}*/
        [HttpGet]
        public HttpResponseMessage Get()
        {
            Person person1 = new Person { PersonId = 1, Name = "Brad", Age = 45 };
            Person person2 = new Person { PersonId = 2, Name = "Mary", Age = 30 };
    
            IEnumerable<Person> data = new List<Person> { person1, person2 };
            JToken json = MyHelper.ToJson(data);
            return Request.CreateResponse<JToken>(json);
        }
    
        //{"Person":{"PersonId":1,"Name":"Brad1","Age":45}} 
        [HttpGet]
        public HttpResponseMessage Get(int id)
        {
            Person data = new Person { PersonId = id, Name = string.Concat("Brad", id), Age = 45 };
            JToken json = MyHelper.ToJson<Person>(data);
            return Request.CreateResponse<JToken>(json);
        }
    }
    
    
    public static class MyHelper
    {
        public static JToken ToJson<T>(T source)
        {
            JObject result = new JObject();
    
            Type type = typeof(T);
            string key = type.Name;
            if (typeof(IEnumerable).IsAssignableFrom(type))
                key = string.Format("ArrayOf{0}", GetEnumeratedType(type).Name);
    
            JToken value = (source != null) ? JToken.FromObject(source) : JValue.CreateNull();//Newtonsoft.Json 6.0.6
            result.Add(key, value);
            return result;
        }
    
        /// <summary>
        /// Use http://stackoverflow.com/questions/4129831/how-do-i-get-the-array-item-type-from-array-type-in-net
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Type GetEnumeratedType(Type type)
        {
            // provided by Array
            var elType = type.GetElementType();
            if (null != elType) return elType;
    
            // otherwise provided by collection
            var elTypes = type.GetGenericArguments();
            if (elTypes.Length > 0) return elTypes[0];
    
            // otherwise is not an 'enumerated' type
            return null;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 2022-11-10
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      相关资源
      最近更新 更多