简体   繁体   中英

Validating Json outputs in c#

I'm trying to validate the output of a json via a url so this is a json which has outputted

{
    "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
            }
        ]
    }
}

and this is one missing the objects but still has an output so my program throws me an error because it can't find the json output I'm trying to use.

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

This is the code I'm using. I'm getting the SteamID from the user which if it's not the correct it will output the json with no objects in the players array but the problem is if my program can't find the json object o["personastate"] it will throw an error. wondering how to counter this? any help appreciated

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
        }
    }
}

You could just check if there are any objects in your ["players"] array (I have not tested this code):

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM