gpt4 book ai didi

c# - 在 Xamarin 中将 json 字符串转换为 .NET 对象

转载 作者:太空狗 更新时间:2023-10-29 23:20:59 27 4
gpt4 key购买 nike

我读过其他几个有这个问题的人,我也尝试过这些方法。但是,我得到了无法确定的奇怪结果。

我正在使用 http://json2csharp.com/#构建我使用 convert 方法的类。

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

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;

我建立了一个方法来从我的 api 调用中获取 json 字符串并使用 Json.NET 将其转换为 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 的调试日志,记录了调用转换后发生的情况。

    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).

最佳答案

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

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

关于c# - 在 Xamarin 中将 json 字符串转换为 .NET 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37646596/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com