c#中json字符串如果value为null的问题

本文关键字:null 问题 value 如果 json 字符串 | 更新日期: 2023-09-27 18:18:50

我使用以下代码:

if (e.Data.MessageArray[0] == "!streams")
{
  try
  {
    WebClient webclient = new WebClient();
    var data = webclient.DownloadString("http://api.justin.tv/api/stream/list.json?channel=dotademon");
    JArray ja = JArray.Parse(data);
    WebClient webclient2 = new WebClient();
    var data2 = webclient2.DownloadString("http://api.justin.tv/api/stream/list.json?channel=trixilulz");
    JArray ja2 = JArray.Parse(data2);
    WebClient webclient3 = new WebClient();
    var data3 = webclient3.DownloadString("http://api.justin.tv/api/stream/list.json?channel=thepremierleague");
    JArray ja3 = JArray.Parse(data3);
    string streamingString = "Live right now: ";
    streamingString += (char)3 + "03EG.Demon" + (char)15 + " - " + "Viewers: " + ja[0]["channel_count"] + " - " + "http://www.justin.tv/dotademon" + (char)3 + "03 Mouz.Trixi" + (char)15 + " - " + "Viewers: " + ja2[0]["channel_count"] + " - " + "http://www.justin.tv/trixilulz" + (char)3 + "03 The Premier League" + (char)15 + " - " + "Viewers: " + ja3[0]["channel_count"] + " - " + "http://www.justin.tv/thepremierleague";
    irc.SendMessage(SendType.Message, e.Data.Channel, streamingString);
    Console.WriteLine("EG.Demon is " + ja[0]["format"]);
    Console.WriteLine("Mouz.Trixi is " + ja[2]["format"]);
    Console.WriteLine("The Premier League is " + ja[3]["format"]);
  }
  catch (ArgumentOutOfRangeException)
  {
    //catch something
  }
}

但是,如果其中一个流不在线,则它根本不输出该字符串。即使2在线,1离线,反之亦然。但是,如果它们都在线,那么它就会正确地输出:

Live right now: EG.Demon - Viewers: 164 - http://www.justin.tv/dotademon Mouz.Trixi - Viewers: 49 - http://www.justin.tv/trixilulz The Premier League - Viewers: 2992 - http://www.justin.tv/thepremierleague
为了演示输出到控制台,下面是代码,它本质上和上面的代码做同样的事情,但是把它发送到控制台,同样的问题,尽管很明显:
using System;
using System.Net;
using Newtonsoft.Json.Linq;
namespace Test
{
  class Program
  {
    static void Main(string[] args)
    {
      try
      {
        WebClient webclient = new WebClient();
        var data = webclient.DownloadString("http://api.justin.tv/api/stream/list.json?channel=dotademon");
        JArray ja = JArray.Parse(data);
        WebClient webclient2 = new WebClient();
        var data2 = webclient2.DownloadString("http://api.justin.tv/api/stream/list.json?channel=trixilulz");
        JArray ja2 = JArray.Parse(data2);
        WebClient webclient3 = new WebClient();
        var data3 = webclient3.DownloadString("http://api.justin.tv/api/stream/list.json?channel=thepremierleague");
        JArray ja3 = JArray.Parse(data3);
        string streamingString = "Live right now: ";
        streamingString += (char)3 + "03EG.Demon" + (char)15 + " - " + "Viewers: " + ja[0]["channel_count"] + " - " + "http://www.justin.tv/dotademon" + (char)3 + "03 Mouz.Trixi" + (char)15 + " - " + "Viewers: " + ja2[0]["channel_count"] + " - " + "http://www.justin.tv/trixilulz" + (char)3 + "03 The Premier League" + (char)15 + " - " + "Viewers: " + ja3[0]["channel_count"] + " - " + "http://www.justin.tv/thepremierleague";
        Console.WriteLine(streamingString);
      }
      catch (ArgumentOutOfRangeException)
      {
        //do something
      }
    }
  }
}
Live right now: EG.Demon - Viewers: 164 - http://www.justin.tv/dotademon Mouz.Trixi - Viewers: 49 - http://www.justin.tv/trixilulz The Premier League - Viewers: 2992 - http://www.justin.tv/thepremierleague

我的问题是,我怎么能使用这作为一个字符串,但输出它仍然,如果它是在线的,而不是输出其余的,如果它是离线的。当其中至少有一个脱机时,它根本不会输出它。如果它在json中找到channel_count,它会检查它是否在线,因为如果它离线,json文件不包含任何内容,只有[]。这是我所知道的唯一检查它是否在线/离线的方法。我用的是JSON。顺便说一下。

c#中json字符串如果value为null的问题

您可以查看ja.Count是否收到响应。

var sb = new StringBuilder("Live right now: ");
if (ja.Count > 0)
  sb.Append(string.Format("EG.Demon - Viewers: {0} - http://www.justin.tv/dotademon", ja[0]["channel_count"]));
if (ja2.Count > 0)
  //...
if (ja3.Count > 0)
  //...
irc.SendMessage(SendType.Message, e.Data.Channel, sb.ToString());