C#的JSON字符串反序列化

编程入门 行业动态 更新时间:2024-10-18 10:32:28
本文介绍了C#的JSON字符串反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我真的在努力寻找JSON反序列化在C#中的任何例子,在每个JSON项目的第一属性是不同的ID。

I am really struggling to find any examples of JSON Deserialization in C# where the first property in each JSON item is a different ID.

{ "12112083":{ "Topic_ID":"12112083", "Moved_ID":"51", "subject":"subject text goes here" }, "12111966":{ "Topic_ID":"12111966", "Moved_ID":"51", "subject":"another subject text" }, "12106917":{ "Topic_ID":"12106917", "Moved_ID":"0", "subject":"test" } }

我已经尝试了几种不同的方法都没有成功。该JSON返回,我无法控制的,有时一个项目,有时像上面的例子项目的数组。理想情况下,我想反序列化到一个类型的对象类,如果可能的。

I have tried a few different methods without success. The JSON returned which I have no control of is sometimes a single item and sometimes an array of items like the example above. Ideally I would like to deserialize into a typed object class if possible.

感谢

由于L.B

我已经试过了code样品提供传递以下字符串

I have tried the code sample provided passing the following string

'{"12112083":{"Topic_ID":"12112083","Moved_ID":"51","subject":"Due to a computer virus, many of my files are \u005C"Crypted\u005C",espe","Start_date":"10/6/2012 6:54:37 PM","InitialResponseDueDate":"10/6/2012 7:01:37 PM","Locked":"0","QValue":"$10","Status":"1","Author_ID":"71318191","Question_Text":"Due to a computer virus, many of my files are \u005C"Crypted\u005C",especially my pictures. Now my FinepixA500 camera will not upload photos to my computer (laptop). Geek Squad could not crack the code and suggested that I contact Toshiba and have them reset the computer to its original manufacturer settings. If I do that, I will lose all of my data. The only thing that appears not to be working is \u005C"photo gallery\u005C". (therefore, my camera will not upload photos anymore.....) Any suggestions?","Username":"JACUSTOMER-iei7o0tr-","No_of_Questions":"0","No_of_Accepts":"0","Last_visit":"10/6/2012 7:22:18 PM","Join_Date":"10/6/2012 6:46:29 PM","lastExpert":"","IsPress":"False","hasCustomerOptOuts":"0","RecCount":"0","OptedOut":"","Urgency":"51","AuthorHasSubscription":"False","Relisted":"0","IsAbandoned":"False","categoryName":"Camera and Video","categoryName_Additional":"Laptop","QuestionCommissionHasAdjusted":"False","LockedBy":"-1","IsOnline":"False","QuestionLink":"/video-camera-repair/77lqr-due-computer-virus-files-crypted-espe.html","WaitingForYou":"False","RequestedForYou":"False","IsChatEnabled":"False","ChangedKeyList":"","DeltaType":"A"},"12111966":{"Topic_ID":"12111966","Moved_ID":"51","subject":"office/outlook for mac 2011 keeps hanging upMicrosoft Error","Start_date":"10/6/2012 6:25:06 PM","InitialResponseDueDate":"10/6/2012 7:25:06 PM","Locked":"0","QValue":"$19","Status":"1","Author_ID":"71317792","Question_Text":"office/outlook for mac 2011 keeps hanging upMicrosoft Error Reporting log version: 2.0<br/><br/>Error Signature:<br/>Exception: EXC_CRASH (SIGTRAP)<br/>Date/Time: 2012-10-06 17:59:38 +0000<br/>Application Name: Microsoft Outlook<br/>Application Bundle ID: com.microsoft.Outlook<br/>Application Signature: OPIM<br/>Application Version: 14.2.4.120824<br/>Crashed Module Name: merp<br/>Crashed Module Version: 2.2.4.120824<br/>Crashed Module Offset: 0x00003aa2<br/>Blame Module Name: Microsoft Outlook<br/>Blame Module Version: 14.2.4.120824<br/>Blame Module Offset: 0x003c900c<br/>Application LCID: 1033<br/>Extra app info: Reg=en Loc=0x0409<br/>Crashed thread: 10","Username":"JACUSTOMER-wr0d1qwb-","No_of_Questions":"0","No_of_Accepts":"0","Last_visit":"10/6/2012 6:54:53 PM","Join_Date":"10/6/2012 6:25:00 PM","lastExpert":"","IsPress":"False","hasCustomerOptOuts":"0","RecCount":"0","OptedOut":"","Urgency":"51","AuthorHasSubscription":"False","Relisted":"0","IsAbandoned":"False","categoryName":"Microsoft Office","categoryName_Additional":"","QuestionCommissionHasAdjusted":"False","LockedBy":"-1","IsOnline":"False","QuestionLink":"/microsoft-office/77lni-office-outlook-mac-2011-keeps-hanging-upmicrosoft-error.html","WaitingForYou":"False","RequestedForYou":"False","IsChatEnabled":"True","ChangedKeyList":"","DeltaType":"A"},"12106917":{"Topic_ID":"12106917","Moved_ID":"0","subject":"test","Start_date":"10/5/2012 6:22:42 PM","InitialResponseDueDate":"10/5/2012 6:40:42 PM","Locked":"0","QValue":"$21","Status":"1","Author_ID":"69617617","Question_Text":"test","Username":"JACUSTOMER-dwbsyao6-","No_of_Questions":"4","No_of_Accepts":"1","Last_visit":"10/6/2012 11:12:19 AM","Join_Date":"7/16/2012 12:59:47 PM","lastExpert":"","IsPress":"False","hasCustomerOptOuts":"0","RecCount":"0","OptedOut":"","Urgency":"0","AuthorHasSubscription":"True","Relisted":"0","IsAbandoned":"False","categoryName":"Computer","categoryName_Additional":"","QuestionCommissionHasAdjusted":"True","LockedBy":"-1","IsOnline":"True","QuestionLink":"/computer/77hr9-test.html","WaitingForYou":"False","RequestedForYou":"False","IsChatEnabled":"True","ChangedKeyList":"","DeltaType":"A"}}'

我收到InvalidCastException的,无法转换类型'System.String'的对象键入Newtonsoft.Json.Linq.JObject。

I received the "InvalidCastException", Unable to cast object of type 'System.String' to type 'Newtonsoft.Json.Linq.JObject'.

我试图消除从一开始到字符串结尾的单引号,但得到了同样的信息?

I tried removing the single quotes from the start and end of the string but got the same message?

推荐答案

使用 Json.Net

(JSON的发布是无效的。下面的作品的在的更换无效的 \ u005C s的 \ u005C )

(The JSON posted is invalid. The following works after replacing invalid \u005C"s with \u005C.)

string json = @"{""12112083"":{""Topic_ID"":""12112083"",""Moved_ID"":""51"",""subject"":""Due to a computer virus, many of my files are \u005CCrypted\u005C,espe"",""Start_date"":""10/6/2012 6:54:37 PM"",""InitialResponseDueDate"":""10/6/2012 7:01:37 PM"",""Locked"":""0"",""QValue"":""$10"",""Status"":""1"",""Author_ID"":""71318191"",""Question_Text"":""Due to a computer virus, many of my files are \u005CCrypted\u005C,especially my pictures. Now my FinepixA500 camera will not upload photos to my computer (laptop). Geek Squad could not crack the code and suggested that I contact Toshiba and have them reset the computer to its original manufacturer settings. If I do that, I will lose all of my data. The only thing that appears not to be working is \u005Cphoto gallery\u005C. (therefore, my camera will not upload photos anymore.....) Any suggestions?"",""Username"":""JACUSTOMER-iei7o0tr-"",""No_of_Questions"":""0"",""No_of_Accepts"":""0"",""Last_visit"":""10/6/2012 7:22:18 PM"",""Join_Date"":""10/6/2012 6:46:29 PM"",""lastExpert"":"""",""IsPress"":""False"",""hasCustomerOptOuts"":""0"",""RecCount"":""0"",""OptedOut"":"""",""Urgency"":""51"",""AuthorHasSubscription"":""False"",""Relisted"":""0"",""IsAbandoned"":""False"",""categoryName"":""Camera and Video"",""categoryName_Additional"":""Laptop"",""QuestionCommissionHasAdjusted"":""False"",""LockedBy"":""-1"",""IsOnline"":""False"",""QuestionLink"":""/video-camera-repair/77lqr-due-computer-virus-files-crypted-espe.html"",""WaitingForYou"":""False"",""RequestedForYou"":""False"",""IsChatEnabled"":""False"",""ChangedKeyList"":"""",""DeltaType"":""A""},""12111966"":{""Topic_ID"":""12111966"",""Moved_ID"":""51"",""subject"":""office/outlook for mac 2011 keeps hanging upMicrosoft Error"",""Start_date"":""10/6/2012 6:25:06 PM"",""InitialResponseDueDate"":""10/6/2012 7:25:06 PM"",""Locked"":""0"",""QValue"":""$19"",""Status"":""1"",""Author_ID"":""71317792"",""Question_Text"":""office/outlook for mac 2011 keeps hanging upMicrosoft Error Reporting log version: 2.0<br/><br/>Error Signature:<br/>Exception: EXC_CRASH (SIGTRAP)<br/>Date/Time: 2012-10-06 17:59:38 +0000<br/>Application Name: Microsoft Outlook<br/>Application Bundle ID: com.microsoft.Outlook<br/>Application Signature: OPIM<br/>Application Version: 14.2.4.120824<br/>Crashed Module Name: merp<br/>Crashed Module Version: 2.2.4.120824<br/>Crashed Module Offset: 0x00003aa2<br/>Blame Module Name: Microsoft Outlook<br/>Blame Module Version: 14.2.4.120824<br/>Blame Module Offset: 0x003c900c<br/>Application LCID: 1033<br/>Extra app info: Reg=en Loc=0x0409<br/>Crashed thread: 10"",""Username"":""JACUSTOMER-wr0d1qwb-"",""No_of_Questions"":""0"",""No_of_Accepts"":""0"",""Last_visit"":""10/6/2012 6:54:53 PM"",""Join_Date"":""10/6/2012 6:25:00 PM"",""lastExpert"":"""",""IsPress"":""False"",""hasCustomerOptOuts"":""0"",""RecCount"":""0"",""OptedOut"":"""",""Urgency"":""51"",""AuthorHasSubscription"":""False"",""Relisted"":""0"",""IsAbandoned"":""False"",""categoryName"":""Microsoft Office"",""categoryName_Additional"":"""",""QuestionCommissionHasAdjusted"":""False"",""LockedBy"":""-1"",""IsOnline"":""False"",""QuestionLink"":""/microsoft-office/77lni-office-outlook-mac-2011-keeps-hanging-upmicrosoft-error.html"",""WaitingForYou"":""False"",""RequestedForYou"":""False"",""IsChatEnabled"":""True"",""ChangedKeyList"":"""",""DeltaType"":""A""},""12106917"":{""Topic_ID"":""12106917"",""Moved_ID"":""0"",""subject"":""test"",""Start_date"":""10/5/2012 6:22:42 PM"",""InitialResponseDueDate"":""10/5/2012 6:40:42 PM"",""Locked"":""0"",""QValue"":""$21"",""Status"":""1"",""Author_ID"":""69617617"",""Question_Text"":""test"",""Username"":""JACUSTOMER-dwbsyao6-"",""No_of_Questions"":""4"",""No_of_Accepts"":""1"",""Last_visit"":""10/6/2012 11:12:19 AM"",""Join_Date"":""7/16/2012 12:59:47 PM"",""lastExpert"":"""",""IsPress"":""False"",""hasCustomerOptOuts"":""0"",""RecCount"":""0"",""OptedOut"":"""",""Urgency"":""0"",""AuthorHasSubscription"":""True"",""Relisted"":""0"",""IsAbandoned"":""False"",""categoryName"":""Computer"",""categoryName_Additional"":"""",""QuestionCommissionHasAdjusted"":""True"",""LockedBy"":""-1"",""IsOnline"":""True"",""QuestionLink"":""/computer/77hr9-test.html"",""WaitingForYou"":""False"",""RequestedForYou"":""False"",""IsChatEnabled"":""True"",""ChangedKeyList"":"""",""DeltaType"":""A""}}"; var jobj = (JObject)JsonConvert.DeserializeObject(json); var items = jobj.Children() .Cast<JProperty>() .Select(j=>new { ID=j.Name, Topic = (string)j.Value["Topic_ID"], Moved = (string)j.Value["Moved_ID"], Subject = (string)j.Value["subject"], }) .ToList();

更多推荐

C#的JSON字符串反序列化

本文发布于:2023-11-17 02:30:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   序列化   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!