none
Access Variables From Different Class

    Question

  • I have my code returning the JSON response that I need - but now how do I access the elements that are returned?  Meaning how can I access gameid, gamename etc from the syntax below?

    namespace GameDataTest
    {
        public class GameInfo
        {
            public int gameid { get; set; }
            public string gamename { get; set; }
            public int cloakid { get; set; }
            public string cms { get; set; }
        }
    
        public class RootObject
        {
            public string GameStyle { get; set; }
            public List<GameInfo> GameInfo { get; set; }
        }
    	
        class Program
        {
    	static void Main(string[] args)
            {
                //
                var response = syncClient.DownloadString(url);
                var o = JsonConvert.DeserializeObject<RootObject[]>(response);
    			//
    	}
       }
    }


    Thursday, April 6, 2017 5:42 PM

Answers

  • If you were to use my renaming suggestion above, that foreach line would read;

    foreach(GameInfo info in ro.GameInfoList)

    So we are going through all the elements in the list, making sure each one is of type GameInfo and calling it "info".

    The reason you are getting that error must be because one or more of the RootElements must have no GameInfos in its list. You should be able to fix it by using a null check to skip those cases.

       // Loop through the GameInfos, if they are not null
       if(ro.GameInfoList != null)
       {
          foreach(GameInfo info in ro.GameInfoList)
          {
             // We can get the id, name and so on like so...
             int id = info.gameid;
             string name = info.gamename;
          }
       }
       else
       {
          // If null lists of GameInfos are not supposed to ever happen,
          // you should take some action here, like popping up an 
          // error message or whatever.
          // If null lists are allowed to happen, you don't need 
          // this "else" block.
       }

    • Marked as answer by IndigoMontoya Saturday, April 8, 2017 11:43 PM
    Saturday, April 8, 2017 1:18 AM

All replies

  • Hi IndigoMontoya,

    Please try the below. Instead of defining "var o", "o" as var type define "o" as "GameInfo" type. Hope this helps you.

    class Program { static void Main(string[] args) { var response = syncClient.DownloadString(url);

    GameInfo o = JsonConvert.DeserializeObject<RootObject[]>(response);

                Console.WriteLine(o.gameid + o.gamename + o.cloakid + o.cms); 

    } }



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Thursday, April 6, 2017 8:48 PM
    Moderator
  • I get an error of

    Cannot implicitly convert type 'RootObject[]' to 'GameInfo'

    Friday, April 7, 2017 1:02 AM
  • Hi IndigoMontoya,

    Thank you for posting here.

    For your question, please try the following code.

    var response = syncClient.DownloadString(url); RootObject o = JsonConvert.DeserializeObject<RootObject[]>(response);

    Console.WriteLine(o.GameStyle);

    For more details, please refer to the links with a simple example.

    http://www.newtonsoft.com/json/help/html/DeserializeObject.htm

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 7, 2017 2:16 AM
    Moderator
  • var o = JsonConvert.DeserializeObject<RootObject[]>(response);
    
    // We have an array of RootObjects, and each RootObject in 
    // the array has a list of GameInfos, and each GameInfo has 
    // a gameid, gamename, and so on.
    
    // Loop through the RootObjects.
    foreach(RootObject ro in o)
    {
       // Loop through the GameInfos.
       foreach(GameInfo info in ro.GameInfo)
       {
          // We can get the id, name and so on like so...
          int id = info.gameid;
          string name = info.gamename;
       }
    }


    By the way, giving a property the same name as a class is generally considered bad practice, because it can cause confusion. With  that in mind, you might consider renaming the list of GameInfos in the RootObject class.

        public class RootObject
        {
            public string GameStyle { get; set; }
            public List<GameInfo> GameInfoList { get; set; } // GameInfoList might be a better name than GameInfo.
        }

    • Edited by Ante Meridian Friday, April 7, 2017 3:47 AM Added the bit about names.
    Friday, April 7, 2017 3:41 AM
  • @Ante Meridian on your line of code that reads

    foreach(GameInfo info in ro.GameInfo)

    Is the GameInfo here pointing to the List<GameInfo> or to the class GameInfo?  (I now see what you mean about naming a property the same as a class causes great confusion).  Reason I ask is that when I try to run my code I get an error of Object reference not set to an instance of an object on ro.GameInfo

    Friday, April 7, 2017 12:38 PM
  • Indigo,

    You simply miss what is a class in C#. A class is in fact a template. It can be a template which is automatically instanced to an object and then it is Static or it has be instanced what is Constructed.

    Also can your return part be in a format which uses a type from the Class which you have mentioned, then you have to get it from that and to cast it to the type (class).

    Many words which means the same but have given endless confusions.

    How to help you, we should know the relation between your returned response and the Gameinfo (in fact how does the response looks like). 


    Success
    Cor


    Friday, April 7, 2017 12:59 PM
  • Indigo,

    You simply miss what is a class in C#. A class is in fact a template. It can be a template which is automatically instanced to an object and then it is Static or it has be instanced what is Constructed.

    Also can your return part be in a format which uses a type from the Class which you have mentioned, then you have to get it from that and to cast it to the type (class).

    Many words which means the same but have given endless confusions.

    How to help you, we should know the relation between your returned response and the Gameinfo (in fact how does the response looks like). 


    Success
    Cor


    This is what the JSON string looks like that is being returned:

    string json = @"[
        {
        'GameStyle':'Adventure',
        'List':
            [
                {
                    'gameid':15,
                    'gamename':'Game 1',
                    'cloakID':10,
                    'cms':'Levy',				
    		'gameid':1225,
                    'gamename':'Game 2',
                    'cloakID':1012,
                    'cms':'Hightail',				
    		'gameid':1881,
                    'gamename':'Game 3',
                    'cloakID':102,
                    'cms':'Bear',
                }
            ]
        }
    ]";

    Friday, April 7, 2017 2:02 PM
  • If you were to use my renaming suggestion above, that foreach line would read;

    foreach(GameInfo info in ro.GameInfoList)

    So we are going through all the elements in the list, making sure each one is of type GameInfo and calling it "info".

    The reason you are getting that error must be because one or more of the RootElements must have no GameInfos in its list. You should be able to fix it by using a null check to skip those cases.

       // Loop through the GameInfos, if they are not null
       if(ro.GameInfoList != null)
       {
          foreach(GameInfo info in ro.GameInfoList)
          {
             // We can get the id, name and so on like so...
             int id = info.gameid;
             string name = info.gamename;
          }
       }
       else
       {
          // If null lists of GameInfos are not supposed to ever happen,
          // you should take some action here, like popping up an 
          // error message or whatever.
          // If null lists are allowed to happen, you don't need 
          // this "else" block.
       }

    • Marked as answer by IndigoMontoya Saturday, April 8, 2017 11:43 PM
    Saturday, April 8, 2017 1:18 AM