2016-04-10 17 views
1

Ich versuche, die Ausgabe eines json über eine URL zu validieren, so ist dies ein json dieValidieren Json Ausgänge in C#

{ 
    "response": { 
     "players": [ 
      { 
       "steamid": "76561197960435530", 
       "communityvisibilitystate": 3, 
       "profilestate": 1, 
       "personaname": "Robin", 
       "lastlogoff": 1460271265, 
       "profileurl": "http://steamcommunity.com/id/robinwalker/", 
       "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4.jpg", 
       "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_medium.jpg", 
       "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_full.jpg", 
       "personastate": 0, 
       "realname": "Robin Walker", 
       "primaryclanid": "103582791429521412", 
       "timecreated": 1063407589, 
       "personastateflags": 0, 
       "loccountrycode": "US", 
       "locstatecode": "WA", 
       "loccityid": 3961 
      } 
     ] 
    } 
} 

ausgegeben hat und dies ist einer der Gegenstände fehlen, hat aber immer noch eine Ausgabe so Mein Programm wirft einen Fehler, weil es die JSON-Ausgabe nicht finden kann, die ich versuche zu verwenden.

{ 
    "response": { 
     "players": [ 
     ] 
    } 
} 

Dies ist der Code, den ich verwende. Ich bekomme die SteamID von dem Benutzer, die, wenn es nicht die richtige ist, wird die JSON ohne Objekte im Player-Array ausgeben, aber das Problem ist, wenn mein Programm das JSON-Objekt o ["personastate"] nicht finden kann ein Fehler. frage mich, wie man dem entgegenwirkt? jede Hilfe dankbar

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

namespace JSONTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string SteamKey = "APIKEY"; 
      Console.WriteLine("Give SteamID Please"); 
      string SteamID = Console.ReadLine(); 

      WebClient c = new WebClient(); 
      var data = c.DownloadString("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/" + "?key=" + SteamKey + "&steamids=" + SteamID); 
      //Console.WriteLine(data); 
      JObject o = JObject.Parse(data); 
      int PrivacyLevel = (int)o["response"]["players"][0]["communityvisibilitystate"]; 
      string username = (string)o["response"]["players"][0]["personaname"]; 
      string ProUrl = (string)o["response"]["players"][0]["profileurl"]; 

      if (PrivacyLevel == 3) 
      { 
       Console.WriteLine("Profile Status: Public"); 
      } 
      else if (PrivacyLevel == 1) 
      { 
       Console.WriteLine("Profile Status: Private"); 
      } 
      else 
      { 
       Console.WriteLine("You have typed the Steam ID Incorrectly"); 
      } 

      Console.ReadLine(); // Don't remove you idiot 
     } 
    } 
} 

Antwort

0

könnten Sie einfach überprüfen, ob es irgendwelche Objekte in Ihrem ["players"] Array sind (ich diesen Code nicht getestet):

JObject o = JObject.Parse(data); 
JArray players = (JArray)o["response"]["players"]; 
if(players.Count > 0){ 
    int PrivacyLevel = (int)players[0]["communityvisibilitystate"]; 
    string username = (string)players[0]["personaname"]; 
    string ProUrl = (string)players[0]["profileurl"]; 

    if (PrivacyLevel == 3) 
    { 
     Console.WriteLine("Profile Status: Public"); 
    } 
    else if (PrivacyLevel == 1) 
    { 
     Console.WriteLine("Profile Status: Private"); 
    } 
} 
else 
{ 
    Console.WriteLine("You have typed the Steam ID Incorrectly"); 
} 
+0

ich nicht einmal daran dachte, es als JArray Aufruf Dank dafür! –