gpt4 book ai didi

c# - Windows Phone 7 中的 JSON 解析

转载 作者:行者123 更新时间:2023-11-30 14:37:57 25 4
gpt4 key购买 nike

因此,我在网上到处查看并获得了一些关于如何解析 JSON 字符串然后将该信息保存到特定变量中的示例,但我对 C# 和 Windows Phone 7 开发非常陌生(只做了一个星期,但我很快就掌握了它,因为我非常了解 C++)。我无法理解我应该如何处理这个问题,所以我只会给你我想要解析的代码和我目前拥有的代码。

我已经使用 http://jsonlint.com/ 验证了我的 JSON 信息验证器。这是我要解析的 JSON 信息(位于网站上):

[
{
"id": 19019,
"model": "tester",
"fields":
{
"name": "thename",
"slot": 45,
"category": "thecategory"
}
}
]

这是我试图用来解析 JSON 信息并将其存储为变量的代码,以便我稍后可以在程序中调用该信息:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Resources;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace PhoneApp
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}

private void myButton_Click(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.OpenReadCompleted += (s, eargs) =>
{
var serializer = new DataContractJsonSerializer(typeof(RootObject));
var theItem = (RootObject)serializer.ReadObject(eargs.Result);
myTextBlock.Text = theItem.pk.ToString();
};
client.OpenReadAsync(new Uri("http://www.example.com/i/19019/json"));
}
}
public class RootObject
{
public int pk { get; set; }
public string model { get; set; }
public Item item { get; set; }
}
public class Item
{
public string name { get; set; }
public int slot { get; set; }
public string category { get; set; }
}
}

就此代码而言,myTextBlock 是我的 Windows Phone 7 应用程序中的一个文本 block ,它具有显示文本的空间,而 myButton 是用户点击以显示文本的按钮(我将一旦我解决了这个问题,就会以不同的方式显示文本)。现在,每当我启动这段代码时,它都会很好地初始化应用程序,但是当我点击按钮时,它会给我一个 InvalidCastException was unhandled 以及具体的当从一个数字转换时,该值必须是小于无穷大的数字。 代码错误:

var theItem = (RootObject)serializer.ReadObject(eargs.Result);

我希望我已经提供了足够的信息来帮助我解决这个问题。我更愿意使用默认情况下内置于 Windows Phone 7 SDK 中的库,但如果使用外部库(如 JSON.NET 或其他兼容的库)可以更好地处理我正在尝试做的事情,那么我会如果您为我提供适当的链接来学习它,我愿意学习。感谢你们提供的任何帮助。

干杯!

最佳答案

您可以使用 Json.Net [抱歉外部库 :(] 反序列化您的输入字符串,如下所示

var root = JsonConvert.DeserializeObject<RootObject[]>(inputString);

public class RootObject
{
public int id { get; set; }
public string model { get; set; }
public Item fields { get; set; }
}
public class Item
{
public string name { get; set; }
public int slot { get; set; }
public string category { get; set; }
}

编辑>>这是完整的源代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace ConsoleApplication1
{
public class MyClass
{
public static void Main(string[] args)
{
string inputString = @"
[
{
""id"": 19019,
""model"": ""tester"",
""fields"":
{
""name"": ""thename"",
""slot"": 45,
""category"": ""thecategory""
}
}
]
";
var root = JsonConvert.DeserializeObject<RootObject[]>(inputString);

foreach (var item in root)
{
Console.WriteLine(item.id + " " + item.model + " " + item.fields.name + " " + item.fields.category);
}
}
}

public class RootObject
{
public int id { get; set; }
public string model { get; set; }
public Item fields { get; set; }
}
public class Item
{
public string name { get; set; }
public int slot { get; set; }
public string category { get; set; }
}
}

关于c# - Windows Phone 7 中的 JSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8639421/

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