转成JSON对象的C#列表 [英] Turn JSON into a C# List of objects

查看:109
本文介绍了转成JSON对象的C#列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,新JSON。读了起来,但不能得到这个工作。我在这里我使用C#中的.aspx页面返回此JSON格式的字符串。

  {ContactsListResult:[{联系:弗雷德·史密斯,使用ContactID:25},{联系:比尔·威尔逊,使用ContactID:45 }]}

我有一个联系对象。

 公共类联系
{
公共字符串联系{搞定;组; }
公众诠释的ContactID {搞定;组; }
}

我需要得到每个联系人到联系对象,进入联系人列表 - 只要使用任何内置框架4.0。我在的JSONContactsListResult位不感兴趣 - 我在方括号中的联系人列表只是有兴趣


解决方案

 使用系统;
使用System.Collections.Generic;
使用System.Web.Script.Serialization;命名空间的ConsoleApplication1 {
   类节目{      [Serializable接口]
      公共类ContactsListResult {
         公共字符串联系{搞定;组; }
         公众诠释的ContactID {搞定;组; }
      } //      [Serializable接口]
      公共类分栏列表{
         公开名单< ContactsListResult> ContactsListResult =新的List< ContactsListResult>();
      } //      静态无效的主要(字串[] args){
         字符串s ={\\ContactsListResult \\:[{\\联系\\:\\弗雷德·史密斯\\,\\的ContactID \\:25},{\\联系\\:\\比尔·威尔逊\\,\\ 的ContactID \\:45}]};         的JavaScriptSerializer lSerializer =新的JavaScriptSerializer();
         分栏列表lItems = lSerializer.Deserialize&所述;分栏列表&将(S);         的foreach(ContactsListResult诉讼中lItems.ContactsListResult)Console.WriteLine(lItem.Contact ++ lItem.ContactID);         到Console.ReadLine();
      } //   } //类
} //命名空间

请跟随我的博客的详细信息:

<一个href=\"http://csharphardcoreprogramming.word$p$pss.com/2014/01/23/json-part-1-basics-follow-up-to-xml/\" rel=\"nofollow\">http://csharphardcoreprogramming.word$p$pss.com/2014/01/23/json-part-1-basics-follow-up-to-xml/

巴斯蒂安M.K.太田

Sorry, new to Json. Have read up but can't get this working. I have this json formatted string being returned on a .aspx page where I am using C#.

{"ContactsListResult":[{"Contact":"Fred Smith","ContactID":25},{"Contact":"Bill Wilson","ContactID":45}]}

I have a Contact object.

public class Contact
{
public string Contact { get; set; }
public int ContactID { get; set; }
}

I need to get each Contact into a Contact object and into a List of Contacts - just using whatever is built into Framework 4.0. I am not interested in the 'ContactsListResult' bit of the Json - I am just interested in the list of contacts within the square brackets.

解决方案

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace ConsoleApplication1 {


   class Program {

      [Serializable]
      public class ContactsListResult {
         public string Contact { get; set; }
         public int ContactID { get; set; }
      } //

      [Serializable]
      public class CList {
         public List<ContactsListResult> ContactsListResult = new   List<ContactsListResult>();
      } //

      static void Main(string[] args) {
         string s = "{\"ContactsListResult\":[{\"Contact\":\"Fred Smith\",\"ContactID\":25},{\"Contact\":\"Bill Wilson\",\"ContactID\":45}]}";

         JavaScriptSerializer lSerializer = new JavaScriptSerializer();
         CList lItems = lSerializer.Deserialize<CList>(s);

         foreach (ContactsListResult lItem in lItems.ContactsListResult) Console.WriteLine(lItem.Contact + " " + lItem.ContactID);

         Console.ReadLine();
      } //

   } // class
} // namespace

Please follow the details on my blog:

http://csharphardcoreprogramming.wordpress.com/2014/01/23/json-part-1-basics-follow-up-to-xml/

Bastian M.K. Ohta

这篇关于转成JSON对象的C#列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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