在WP7中用JSON信息解析2个不同的网站

本文关键字:2个 网站 WP7 中用 JSON 信息 | 更新日期: 2023-09-27 18:19:50

我被这个问题困扰的时间比我愿意承认的要长。让我解释一下我想做什么。我有一个网站,里面有很多拍卖,它们的数据存储在JSON文件中。我已经了解了如何使用JSON.net的反序列化程序解析JSON数据。现在,我需要做的是从拍卖列表中获取我解析的数据,然后在每次拍卖中使用物品id从另一个网站获取更详细的信息。以下是我目前所拥有的:

public AuctionSearch()
{
    InitializeComponent();
    WebClient proxy = new WebClient();
    proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(get_auction_info);
    proxy.OpenReadAsync(new Uri("http://www.example.com/auctions/"));
}
void get_auction_info(object sender, OpenReadCompletedEventArgs e)
{
    Stream stream = e.Result;
    StreamReader sr = sr.ReadToEnd();
    var root = JsonConvert.DeserializeObject<AuctionListing>(result);
}

以下是我目前用于存储拍卖信息的类:

public class AuctionListing
{
    public long auctionID { get; set; }
    public string itemName { get; set; }
    public long itemID { get; set; }
    public string owner { get; set; }
    public long bid { get; set; }
    public long buyout { get; set; }
    public long quantity { get; set; }
    public string timeLeft { get; set; }
}

现在,这是我的问题。我正在尝试使用从JSON解析中获得的项目ID#,并从另一个网站获得项目名称,该网站将显示项目名称和有关该项目的一系列其他信息。URL应该类似http://www.newexample.com/i/%itemID%/json然后我必须解析这些信息。一旦我解析了它,我就会遇到一个问题,即每次拍卖的名称与其他物品信息相匹配。我有基本上与上面相同的函数,但解析到不同的类(当然)。我希望我已经提供了足够的信息来帮助别人。如果我需要在这里发布我的完整代码,我可以,但它相当长。我认为我的问题与OpenReadAsync的工作方式有关,但我不确定。如果你们还有其他问题,请告诉我。

澄清

我知道如何获取商品名称。我的问题是,我想存储来自两个解析的所有信息。下面是我的源代码。我会解释一下之后会发生什么。

(continuation of the code from above)
void get_auction_info(object sender, OpenReadCompletedEventArgs e)
{
    (stuff from above)
    for (int i = 0; i < root[i].Length; i++)
    {
        WebClient client = new WebClient();
        client.OpenReadCompleted += new OpenReadCompletedEventHandler(auction_information);
        client.OpenReadAsync(new Uri("http://www.newexample.com/i/" + root[i].itemID + "/json"));
        TextBlock tb = new Textblock();
        tb.Text = (
                    "Auction ID #: " + root[i].auctionID +
                    "'nItem ID: " + root[i].itemID + "'n'n"
                  );
        tb.TextWrapping = TextWrapping.NoWrap;
        TextPanel.Children.Add(tb);
    }
}
void aucton_information(object sender, OpenReadCompletedEventArgs e)
{
    Stream stream = e.Result;
    StreamReader itemStreamReader = new StreamReader(stream);
    string theResult = itemStreamReader.ReadToEnd();
    var secondRoot = JsonConvert.DeserializeObject<AuctionInfo>(theResult);
    stream.Close();
    itemStreamReader.Close();
}

现在,我的问题是这个。一旦我从auction_information OpenReadAsync调用第二个URL,它就不会像我期望的那样工作。我想做的是能够从给定的itemID中获取项目名称,然后将其设置为itemName的变量。我不明白如何返回刚刚从第二次JSON解析中获得的项名称,并将其设置为第一次JSON调用中根对象中的itemID的值。我希望这能有所帮助。

在WP7中用JSON信息解析2个不同的网站

我不太清楚你的问题,但你想在uri中包含你的itemID吗?

你可以用和拍卖一样的方式来做:

public ItemSearch() 
{ 
    InitializeComponent(); 
    WebClient proxy = new WebClient(); 
    proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(get_item_info); 
    proxy.OpenReadAsync(new Uri("http://www.example.com/i/" + auction.itemID.ToString() + "/json")); 
} 

如果问题是在用于解析特定项的回调中获取itemID,则可以使用eventargs e来获取uri并从中筛选id