Xamarin.Forms将json反序列化为对象 [英] Xamarin.Forms deserialising json to an object

查看:412
本文介绍了Xamarin.Forms将json反序列化为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var myObjectList = (List<MyObject>)JsonConvert.DeserializeObject(strResponseMessage, typeof(List<MyObject>));

当JSON具有以下格式时,以上方法可将JSON字符串反序列化为自定义对象列表

the above works to deserialise a JSON string to a list of custom objects when the JSON has the following format

[
   {
     "Name": "Value"
   },
   {
     "Name": "Value"
   },
   {
     "Name": "Value"
   },
   "Name": "Value"
   }
]

这种格式时,我不知道该怎么做

I don't know how to do the same when the format is like this

{
    "ReturnedData" : [
       {
         "Name": "Value"
       },
       {
         "Name": "Value"
       },
       {
         "Name": "Value"
       },
       "Name": "Value"
       }
    ]

}

我可以得到这样的数据

JObject information = JObject.Parse(strResponseMessage);
foreach (dynamic data in information)
{
   //convert to object here
}

并且适用于Android,但由于出现错误,您似乎无法为iOS使用动态"类型:

and that works for Android but it seems that you cannot use a type of 'dynamic' for iOS as I get the error:

Object type Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder cannot be converted to target type: System.Object[]

将第二个JSON字符串转换为第一个JSON缺少什么步骤?

What step am I missing to convert the second JSON string to the first?

推荐答案

感谢您的答复-我能够按照以下说明解决问题

Thanks for your replies - I was able to solve it as follows

JObject information = JObject.Parse(strResponseMessage);

string json = JsonConvert.SerializeObject(strResponseMessage["ReturnedData "]);

var myObjectList = (List<MyObject>)JsonConvert.DeserializeObject(json , typeof(List<MyObject>));

完美工作!

这篇关于Xamarin.Forms将json反序列化为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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