在Xamarin中将JSON字符串转换为.NET对象

6

我读过几篇其他人提出的这个问题,我尝试了那些方法。然而,我得到了一些奇怪的结果,无法确定原因。

我正在使用http://json2csharp.com/#来构建我用于转换方法的类。

在我的代码中,我正在使用以下库:

using System;
using RestSharp;
using Newtonsoft.Json;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using ZXing;
using ZXing.Mobile;

我有一个方法,它可以使用Json.NET将我的API调用中的JSON字符串转换为C#对象。

public void convert(String response)
{
    Console.WriteLine("Response: " + response);
    Console.WriteLine("Check 1");
    var list = JsonConvert.DeserializeObject<List<RootObject>>(response);
    Console.WriteLine("Check 2");
    Console.WriteLine(list);
    Console.WriteLine("Check 3");
    for (int i = 0; i < list.Count; i++) // Loop with for.
    {
        Console.WriteLine("Check 4");
        Console.WriteLine("Pass: " + i);
        Console.WriteLine(list[i]);
    }
    Console.WriteLine("Check 5");
}

public class Item
{
    public string barcode { get; set; }
    public string name { get; set; }
    public string upvotes { get; set; }
    public string downvotes { get; set; }
    public string updated { get; set; }
}

public class RootObject
{
    public List<Item> items { get; set; }
}

这是我调用的API的示例数据集:
{
  "items":[
   {
     "barcode": "12345",
     "name": "xxx",
     "upvotes": "0",
     "downvotes": "0",
     "updated": "1465103701673"
  },
  {
     "barcode": "1234",
     "name": "xxx",
     "upvotes": "0",
     "downvotes": "0",
     "updated": "1465103697375"
   },
   {
     "barcode": "123",
     "name": "xxx",
     "upvotes": "0",
     "downvotes": "0",
     "updated": "1465103688554"
   }
]}

错误日志中没有弹出任何错误。

如果我尝试在达到这行代码时添加调试功能

var list = JsonConvert.DeserializeObject<List<RootObject>>(response);

我收到了“模块中没有框架”的信息

代码只执行到检查1,然后似乎停止了。下面是我从Visual Studio的调试日志中得出的,在我调用convert后发生的情况。

    06-05 16:32:59.391 D/Mono    ( 3672): Assembly Ref addref PennyGeneral[0x813fcad0] -> Newtonsoft.Json[0x82201ac8]: 2
    06-05 16:32:59.391 D/Mono    ( 3672): The request to load the retargetable assembly mscorlib v2.0.5.0 was remapped to mscorlib v2.0.5.0
    06-05 16:32:59.391 D/Mono    ( 3672): Assembly Ref addref Newtonsoft.Json[0x82201ac8] -> mscorlib[0x76d8a880]: 12
    Response: {"items":[{"barcode": "12345","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103701673"},{"barcode": "1234","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103697375"},{"barcode": "123","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103688554"}]}
    06-05 16:32:59.392 I/mono-stdout( 3672): Response: {"items":[{"barcode": "12345","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103701673"},{"barcode": "1234","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103697375"},{"barcode": "123","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103688554"}]}
    Check 1
    06-05 16:32:59.393 I/mono-stdout( 3672): Check 1
    06-05 16:32:59.393 D/Mono    ( 3672): Remapped public key token of retargetable assembly System.Core from 7cec85d7bea7798e to b77a5c561934e089
    06-05 16:32:59.393 D/Mono    ( 3672): The request to load the retargetable assembly System.Core v2.0.5.0 was remapped to System.Core v2.0.5.0
    06-05 16:32:59.394 D/Mono    ( 3672): Unloading image System.Core.dll [0x85f686d0].
    06-05 16:32:59.394 D/Mono    ( 3672): Image addref System.Core[0x85f660f0] -> System.Core.dll[0x82d98df8]: 4
    06-05 16:32:59.395 D/Mono    ( 3672): Config attempting to parse: 'System.Core.dll.config'.
    06-05 16:32:59.395 D/Mono    ( 3672): Config attempting to parse: '/Users/builder/data/lanes/3236/ee215fc9/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/System.Core/System.Core.config'.
    06-05 16:32:59.395 D/Mono    ( 3672): Assembly Ref addref Newtonsoft.Json[0x82201ac8] -> System.Core[0x82df1b18]: 4
    06-05 16:32:59.427 D/Mono    ( 3672): Remapped public key token of retargetable assembly System from 7cec85d7bea7798e to b77a5c561934e089
    06-05 16:32:59.427 D/Mono    ( 3672): The request to load the retargetable assembly System v2.0.5.0 was remapped to System v2.0.5.0
    06-05 16:32:59.428 D/Mono    ( 3672): Unloading image System.dll [0x85fb77d0].
    06-05 16:32:59.428 D/Mono    ( 3672): Image addref System[0x85fb94b0] -> System.dll[0x830252a0]: 4
    06-05 16:32:59.428 D/Mono    ( 3672): Config attempting to parse: 'System.dll.config'.
    06-05 16:32:59.428 D/Mono    ( 3672): Config attempting to parse: '/Users/builder/data/lanes/3236/ee215fc9/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/System/System.config'.
    06-05 16:32:59.428 D/Mono    ( 3672): Assembly Ref addref Newtonsoft.Json[0x82201ac8] -> System[0x8302f6e0]: 4
    06-05 16:32:59.475 D/Mono    ( 3672): Remapped public key token of retargetable assembly System.Runtime.Serialization from 7cec85d7bea7798e to b77a5c561934e089
    06-05 16:32:59.475 D/Mono    ( 3672): The request to load the retargetable assembly System.Runtime.Serialization v2.0.5.0 was remapped to System.Runtime.Serialization v2.0.5.0
    06-05 16:32:59.476 D/Mono    ( 3672): Image addref System.Runtime.Serialization[0x86002d60] -> System.Runtime.Serialization.dll[0x86002098]: 1
    06-05 16:32:59.476 D/Mono    ( 3672): Assembly System.Runtime.Serialization[0x86002d60] added to domain RootDomain, ref_count=1
    06-05 16:32:59.476 D/Mono    ( 3672): AOT module 'System.Runtime.Serialization.dll.so' not found: dlopen failed: library "/data/app-lib/PennyGeneral.PennyGeneral-2/libaot-System.Runtime.Serialization.dll.so" not found
    06-05 16:32:59.477 D/Mono    ( 3672): AOT module '/Users/builder/data/lanes/3236/ee215fc9/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/System.Runtime.Serialization.dll.so' not found: dlopen failed: library "/data/app-lib/PennyGeneral.PennyGeneral-2/libaot-System.Runtime.Serialization.dll.so" not found
    06-05 16:32:59.477 D/Mono    ( 3672): Unloading image data-0x8602f008 [0x86002f78].
    06-05 16:32:59.478 D/Mono    ( 3672): Config attempting to parse: 'System.Runtime.Serialization.dll.config'.
    06-05 16:32:59.478 D/Mono    ( 3672): Config attempting to parse: '/Users/builder/data/lanes/3236/ee215fc9/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/System.Runtime.Serialization/System.Runtime.Serialization.config'.
    06-05 16:32:59.478 D/Mono    ( 3672): Assembly Ref addref Newtonsoft.Json[0x82201ac8] -> System.Runtime.Serialization[0x86002d60]: 2
    Loaded assembly: System.Runtime.Serialization.dll [External]
    06-05 16:32:59.478 D/Mono    ( 3672): Assembly Ref addref System.Runtime.Serialization[0x86002d60] -> mscorlib[0x76d8a880]: 13
    06-05 16:32:59.500 D/Mono    ( 3672): Assembly Ref addref System.Core[0x82df1b18] -> System[0x8302f6e0]: 5
    Loaded assembly: Anonymously Hosted DynamicMethods Assembly [External]
    Thread finished: <Thread Pool> #5
    06-05 16:33:09.774 D/Mono    ( 3672): [0x834fab30] worker finishing
    06-05 16:33:09.781 D/dalvikvm( 3672): threadid=16: bye!
    The thread 'Unknown' (0x5) has exited with code 0 (0x0).
    Thread finished: <Thread Pool> #7
    06-05 16:33:37.391 D/Mono    ( 3672): [0x85274538] worker finishing
    06-05 16:33:37.398 D/dalvikvm( 3672): threadid=20: bye!
    The thread 'Unknown' (0x7) has exited with code 0 (0x0).
    Thread finished: <Thread Pool> #8
    06-05 16:33:53.046 D/Mono    ( 3672): [0x851f1b60] worker finishing
    06-05 16:33:53.052 D/dalvikvm( 3672): threadid=19: bye!
    The thread 'Unknown' (0x8) has exited with code 0 (0x0).
    Thread finished: <Thread Pool> #3
    06-05 16:33:59.536 D/dalvikvm( 3672): threadid=15: bye!
    The thread 'Unknown' (0x3) has exited with code 0 (0x0).
    Thread started: <Thread Pool> #10
    Thread started: <Thread Pool> #11
    06-05 16:34:39.062 D/dalvikvm( 3672): threadid=15: interp stack at 0x83950000
    06-05 16:34:39.064 D/dalvikvm( 3672): threadid=16: interp stack at 0x83970000
    06-05 16:34:39.065 D/Mono    ( 3672): [0x834fa728] worker starting
    Thread finished: <Thread Pool> #11
    06-05 16:34:59.711 D/Mono    ( 3672): [0x834fa728] worker finishing
    06-05 16:34:59.717 D/dalvikvm( 3672): threadid=16: bye!
    The thread 'Unknown' (0xb) has exited with code 0 (0x0).
1个回答

6

您的根对象存储了项目的集合。将其更改为...

var root = JsonConvert.DeserializeObject<RootObject>(response);
var list = root.items;

你是我新的最喜欢的人。计时器结束后,我会将其标记为已接受并感谢你。谢谢。 - StevenDStanton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接