none
JSON output not appearing as an array but a larger string, need advice RRS feed

  • Question

  • Hi All

    I am creating an api that outputs a json, but my app that executes the get command receive only a JSON string but if the app was to execute anther api (which i have not written) the json comes as array and each element is correctly created and accessible.

    The code below is a short version of what i am doing.

    public class Zonetable
        {
            public int Row { get; set; }
            public int Mode { get; set; }
            public int DeviceA { get; set; }
            public int DeviceB { get; set; }
            public int PumpDevice { get; set; }
            public int FertDevice { get; set; }
            public int LevelA { get; set; }
            public int LevelB { get; set; }
            public int FLevelA { get; set; }
            public int FLevelB { get; set; }
            public int solarlight { get; set; }
            public int Counter { get; set; }
            public int Options { get; set; }
            public string DeviceName { get; set; }
            public string DeviceNameField {get;set;}        
            public string Name {get; set;}
            public int FailStatus {get; set;}
            public int Status {get; set;}
        }
    
     public string  sql_run(string id)
    {
     while (rdr.Read())
                    {
             
    
                        zoneinst.DeviceName = (string)rdr.GetString(0);
                        zoneinst.DeviceNameField = (string)rdr.GetString(1);
                        zoneinst.Row = (int)rdr.GetInt32(2);
                        zoneinst.Mode = (int)rdr.GetInt32(3);
                        zoneinst.DeviceA = (int)rdr.GetInt32(4);
                        zoneinst.DeviceB = (int)rdr.GetInt32(5);
                        zoneinst.PumpDevice = (int)rdr.GetInt32(6);
                        zoneinst.FertDevice = (int)rdr.GetInt32(7);
                        zoneinst.LevelA = (int)rdr.GetInt32(8);
                        zoneinst.LevelB = (int)rdr.GetInt32(9);
                        zoneinst.FLevelA = (int)rdr.GetInt32(10);
                        zoneinst.FLevelB = (int)rdr.GetInt32(11);
                        zoneinst.Counter = (int)rdr.GetInt32(12);
                        zoneinst.Options = (int)rdr.GetInt32(13);
                        zoneinst.solarlight = (int)rdr.GetInt32(14);
                        zoneinst.Name = (string)rdr.GetString(17);
                        zoneinst.FailStatus = (int)rdr.GetInt32(24);
                        zoneinst.Status = (int)rdr.GetInt32(25);
    
                        string json = JsonConvert.SerializeObject(zoneinst, Formatting.None);
                        zonelist.Add(json);                                                          
                       
    
    
                    }
    
    
    
    }
          // GET api/Zones
            public string  Get(string id)
            {
                
                return sql_run(id);
    
                
            }
    
    
    

    Can someone advice me where i am going wrong?


    • Edited by DEE_JAY Monday, November 20, 2017 11:58 AM typing error
    Monday, November 20, 2017 11:57 AM

All replies

  • Hi All

    I am creating an api that outputs a json, but my app that executes the get command receive only a JSON string but if the app was to execute anther api (which i have not written) the json comes as array and each element is correctly created and accessible.

    The code below is a short version of what i am doing.

    public class Zonetable
        {
            public int Row { get; set; }
            public int Mode { get; set; }
            public int DeviceA { get; set; }
            public int DeviceB { get; set; }
            public int PumpDevice { get; set; }
            public int FertDevice { get; set; }
            public int LevelA { get; set; }
            public int LevelB { get; set; }
            public int FLevelA { get; set; }
            public int FLevelB { get; set; }
            public int solarlight { get; set; }
            public int Counter { get; set; }
            public int Options { get; set; }
            public string DeviceName { get; set; }
            public string DeviceNameField {get;set;}        
            public string Name {get; set;}
            public int FailStatus {get; set;}
            public int Status {get; set;}
        }
    
     public string  sql_run(string id)
    {
     while (rdr.Read())
                    {
             
    
                        zoneinst.DeviceName = (string)rdr.GetString(0);
                        zoneinst.DeviceNameField = (string)rdr.GetString(1);
                        zoneinst.Row = (int)rdr.GetInt32(2);
                        zoneinst.Mode = (int)rdr.GetInt32(3);
                        zoneinst.DeviceA = (int)rdr.GetInt32(4);
                        zoneinst.DeviceB = (int)rdr.GetInt32(5);
                        zoneinst.PumpDevice = (int)rdr.GetInt32(6);
                        zoneinst.FertDevice = (int)rdr.GetInt32(7);
                        zoneinst.LevelA = (int)rdr.GetInt32(8);
                        zoneinst.LevelB = (int)rdr.GetInt32(9);
                        zoneinst.FLevelA = (int)rdr.GetInt32(10);
                        zoneinst.FLevelB = (int)rdr.GetInt32(11);
                        zoneinst.Counter = (int)rdr.GetInt32(12);
                        zoneinst.Options = (int)rdr.GetInt32(13);
                        zoneinst.solarlight = (int)rdr.GetInt32(14);
                        zoneinst.Name = (string)rdr.GetString(17);
                        zoneinst.FailStatus = (int)rdr.GetInt32(24);
                        zoneinst.Status = (int)rdr.GetInt32(25);
    
                        string json = JsonConvert.SerializeObject(zoneinst, Formatting.None);
                        zonelist.Add(json);                                                          
                       
    
    
                    }
    
    
    
    }
          // GET api/Zones
            public string  Get(string id)
            {
                
                return sql_run(id);
    
                
            }
    
    

    Can someone advice me where i am going wrong?


    Dee Jay we cannot understand what your question is. Tell us what you are trying to get and what you are getting. Type up a little example.

    My Technet Articles

    If you like this or another reply, vote it up!
    If you think this or another reply answers the original question, mark it or propose it as an answer.


    Mauricio Feijo
    www.mauriciofeijo.com

    Monday, November 20, 2017 1:23 PM
  • Your conversion code is not correct, that is why it is producing bad JSON. Each time through your loop you're serializing the object to JSON. This products a string with a single object in it. You didn't post the entire code for that method so we cannot tell what you are ultimately doing with it. Right now it appears you're just adding it to a list of strings. I assume you're then trying to return the list of strings but that won't return an array of JSON objects. It'll return a bunch of JSON objects.

    Alter your read code to not convert to JSON at all. Instead, read each object and store it in a List<Zonetable>. That is what you should return from your sql_run method. In your API method, call that method and get the results back. Then use JsonConvert on the results to convert the list to an array of JSON objects that gets returned. 

    public IEnumerable<Zonetable> sql_run ( string id )
    {
       var items = new List<Zonetable>();
    
       ...
       while (rdr.Read())
       {
          var item = new Zonetable();
          item.DeviceName = ...;
    
          items.Add(item);
       };
    
       return items;
    }
    
    public string Get ( string id )
    {
       var items = sql_run(id);
     
       return JsonConvert.SerializeObject(items);
    }

    Please post any further questions related to WEB API or web development in the ASP.NET forums.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, November 20, 2017 3:05 PM
    Moderator
  • Hi DEF_JAY,

    Thank you for posting here.

    For your question, you could download the source file from the code project for reference. It shows how to convert any object or object list into JSON, we have to use the function JsonConvert.SerializeObject. The example demonstrates the use of JSON in an ASP.NET environment.

    https://www.codeproject.com/Articles/78928/Create-JSON-from-C-using-JSON-Library

    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.

    Wednesday, November 22, 2017 9:44 AM
    Moderator