在Windows Phone 7中的JSON解析

问题描述:

因此,我在网上随处看到了几个关于如何解析JSON字符串并将该信息保存到特定变量的示例,但我对C#和Windows Phone 7开发极其新颖(仅限于一直这样做了一个星期,但我很快就采用它,因为我很了解C++)。我很难理解我应该如何处理这个问题,所以我只会给你我想解析的代码和我到目前为止的代码。在Windows Phone 7中的JSON解析

我已经使用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应用程序中的文本块具有显示文本的空间和myButton为按钮的用户点击以显示文本(一旦我修正了这个问题,我将以不同的方式显示文本)。现在,只要我启动这个代码,它就会初始化应用程序,但是当我点击按钮时,它给了我一个InvalidCastException未处理与具体当从一个数字投射时,该值必须是一个数字少一些比无限。错误,代码:

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; } 
    } 
} 
+0

这将是有益的,除了我甚至无法将JSON字符串放入这些变量中。我不知道什么是错的。 :/ – th3n3wguy 2011-12-27 18:29:18

+0

是关于JSON序列化或如何获取JSON字符串的问题吗?或者是其他东西? – 2011-12-27 20:23:25

+0

我会老实说,我不太明白什么时候需要序列化以及何时反序列化。我想要做的就是获取JSON文件中每个字段的值,并将该信息保存到一个变量中,以便我可以用自己的方式显示它,而不是仅以字符串形式显示。这有帮助吗? – th3n3wguy 2011-12-28 20:25:11

这里有一种解析方法,不需要外部库

[DataContract] 
public class InfoJson 
{ 
    [DataMember] 
    public int id { get; set; } 

    public static InfoJson[] parseArray(String json) 
    { 
     var serializer = new DataContractJsonSerializer(typeof(InfoJson[])); 

     using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) 
     { 
      return (InfoJson[]) serializer.ReadObject(ms); 
     } 
    } 
} 

下面的代码很简单,完整的代码使用HTTP客户端获取来自网络的响应,并对其进行分析:

var httpClient = new HttpClient(new HttpClientHandler()); 

var url ="http://www.example.com/i/19019/json"; 

HttpResponseMessage response = await httpClient.GetAsync(url); 

        var data = response.Content.ReadAsStringAsync(); 

        var result= JsonConvert.DeserializeObject<RootObject>(data.Result.ToString()); 

您可以访问数据成员为:

result.id;