如何将json传递给C#中的列表对象 [英] How to convet json to list object in C#

查看:277
本文介绍了如何将json传递给C#中的列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我有一节课



公共课myPracticeInfo 
{
public long CandidateID {get;组; }

public long CandidateCertificateID {get;组; }


public long CandidateCertificateModuleID {get;组; }


public int PracticeTimeInMinutes {get;组; }

public int LangugaeID {get;组; }


public int NumberOfQuestions {get;组; }


公共布尔? IsActive {得到;组; }
}





我有一个字符串json,



 [{'CandidateID':'17','CandidateCertificateID':'21','CandidateCertificateModuleID':'39','PracticeTimeInMinutes':'20','LangugaeID':'1', 'NumberOfQuestions':'20','IsActive':'true'}] 





我的尝试:



 public static string CallSavePracticeInfo()
{
var practiceInfo = HttpContext.Current.Request.QueryString [ practiceInfo];
// practiceInfo = practiceInfo.Replace(',\);
var pInfo = JsonConvert.SerializeObject(practiceInfo);
var model = JsonConvert.DeserializeObject< myPracticeInfo> (pInfo);
返回模型!= null?1:0;
}





但上面的代码抛出错误



 Newtonsoft.Json.JsonSerializationException:'转换值时出错[{'CandidateID':' 17','CandidateCertificateID':'21','CandidateCertificateModuleID':'39','PracticeTimeInMinutes':'20','LangugaeID':'1','NumberOfQuestions':'20','IsActive':'true' }'键入'Task.UtilityTask + myPracticeInfo'。路径'',第1行,位置193.'

内部异常:
ArgumentException:无法从System.String转换或转换to myPracticeInfo。





任何人都可以帮助我。





提前谢谢。

解决方案

我写了一篇文章来回答这些问题:在C#中使用JSON& VB [ ^ ]



它将提供您需要的所有答案。


您是否正在寻找这样的结果 - < br $> b $ b

如何将Json数组转换为c#中的对象列表 - Stack Overflow [ ^ ]



javascript - 将Json字符串转换为C#对象列表 - 堆栈溢出 [ ^

Dear All,

I have one class

public class myPracticeInfo
        {
            public long CandidateID { get; set; }
            
            public long CandidateCertificateID { get; set; }
            

            public long CandidateCertificateModuleID { get; set; }
            

            public int PracticeTimeInMinutes { get; set; }

            public int LangugaeID { get; set; }
            

            public int NumberOfQuestions { get; set; }
            

            public bool? IsActive { get; set; }
        }



and i have a string json,

[{ 'CandidateID': '17', 'CandidateCertificateID': '21', 'CandidateCertificateModuleID': '39', 'PracticeTimeInMinutes': '20', 'LangugaeID': '1', 'NumberOfQuestions': '20', 'IsActive': 'true'}]



What I have tried:

public static string CallSavePracticeInfo()
       {
           var practiceInfo = HttpContext.Current.Request.QueryString["practiceInfo"];
           //practiceInfo = practiceInfo.Replace("'", "\"");
           var pInfo = JsonConvert.SerializeObject(practiceInfo);
           var model = JsonConvert.DeserializeObject<myPracticeInfo>(pInfo);
           return model != null ? "1" : "0";
       }



But the above code throws error

Newtonsoft.Json.JsonSerializationException: 'Error converting value "[{ 'CandidateID': '17', 'CandidateCertificateID': '21', 'CandidateCertificateModuleID': '39', 'PracticeTimeInMinutes': '20', 'LangugaeID': '1', 'NumberOfQuestions': '20', 'IsActive': 'true'}]" to type 'Task.UtilityTask+myPracticeInfo'. Path '', line 1, position 193.'

Inner Exception:
ArgumentException: Could not cast or convert from System.String to myPracticeInfo.



Can anyone please help me.


Thanks in advance.

解决方案

I wrote an article to answer these questions: Working with JSON in C# & VB[^]

It will have all the answer that you need.


Are you looking something like this -

How to convert Json array to list of objects in c# - Stack Overflow[^]

javascript - Convert Json String to C# Object List - Stack Overflow[^]


这篇关于如何将json传递给C#中的列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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