当前位置: 首页 > 编程语言 > C#

当数组表示为具有“item#XXX”属性的对象时,阅读JSON分享

时间:2023-04-11 01:29:13 C#

当数组表示为具有“item#XXX”属性的对象时,阅读JSON我有这个JSON文本,我不知道如何解析“items”属性来填充项目列表{“response”:{“success”:1,“current_time”:1445015502,“items”:{“item1”:{"property1":1,"property2":"test","property3":4.3},"item2":{"property1":5,"property2":"test2","property3":7.8}}}}这些是我的课程:publicclassItem{publicintproperty1{get;放;}publicstringproperty2{得到;放;}publicdoubleproperty3{get;放;}}publicclassResponse{publicintsuccess{get;放;}公共字符串消息{得到;放;}publicintcurrent_time{得到;放;}公共列表项{得到;放;}}publicclassRootObject{publicResponseresponse{get;放;另外,不,这不是错误。JSON文本中没有[nor]。此外,JSON中的项数未定义。感谢Json.NET和动态,这很容易做到:RootObjectparsed=newRootObject(){response=newResponse(){success=jsonObject.response.success,current_time=jsonObject.response.current_time,message=jsonObject.response.message,items=ParseItems(jsonObject.response.items)}};返回解析;}privateListParseItems(dynamicitems){ListitemList=newList();foreach(variteminitems){itemList.Add(newItem(){property1=item.Value.property1,property2=item.Value.property2,property3=item.Value.property3});}返回项目列表;}items不是JSON中的数组,它是一个对象:"items":{"item1":{"property1":1,"property2":"test","property3":4.3},"item2":{"property1":5,"property2":"test2","property3":7.8}}所以它不会反序列化为一个集合:publicListitems{get;放;}而是为它创建一个类型:publicclassItems{publicItemitem1{get;放;}publicItem吧m2{得到;放;}}并在父对象中使用:以上为C#学习教程:当数组表示为具有“item#XXX”属性的对象时,请阅读JSON分享的全部内容,如果有的话正在使用,需要了解更多C#学习教程,希望大家多多关注——publicclassResponse{publicintsuccess{get;放;}公共字符串消息{得到;放;}publicintcurrent_time{得到;放;}publicItemsitems{get;放;}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: