将Smartsheet数据导入Gridview

时间:2015-06-23 04:35:27

标签: c# asp.net json gridview smartsheet-api

我对Smartsheet很新,使用c#进行编程。主要是初学者,并尝试使用visual studio 2013将我的Web应用程序与Smartsheet集成。我目前正在努力获得一个gridview控件来填充来自Smartsheet的返回数据(以json格式)。到目前为止,我已经能够获得包含以下内容的json字符串:

{  
   "id":4705121406871428,
   "name":"Distributors",
   "version":3,
   "totalRowCount":2,
   "accessLevel":"OWNER",
   "effectiveAttachmentOptions":[  
      "DROPBOX",
      "GOOGLE_DRIVE",
      "BOX_COM",
      "FILE"
   ],
   "ganttEnabled":false,
   "dependenciesEnabled":false,
   "permalink":"https://app.smartsheet.com/b/home?lx=xlJ7OtqknyOndgOnClj4qg",
   "createdAt":"2015-06-22T16:55:02+10:00",
   "modifiedAt":"2015-06-23T14:17:47+10:00",
   "columns":[  
      {  
         "id":3238895438587780,
         "index":0,
         "title":"id",
         "type":"TEXT_NUMBER",
         "primary":true,
         "width":150
      },
      {  
         "id":7742495065958276,
         "index":1,
         "title":"distName",
         "type":"TEXT_NUMBER",
         "width":150
      },
      {  
         "id":2112995531745156,
         "index":2,
         "title":"agency",
         "type":"TEXT_NUMBER",
         "width":150
      },
      {  
         "id":6616595159115652,
         "index":3,
         "title":"profile",
         "type":"TEXT_NUMBER",
         "width":150
      }
   ],
   "rows":[  
      {  
         "id":3999757174630276,
         "rowNumber":1,
         "expanded":true,
         "createdAt":"2015-06-23T11:05:26+10:00",
         "modifiedAt":"2015-06-23T14:17:47+10:00",
         "cells":[  
            {  
               "columnId":3238895438587780,
               "type":"TEXT_NUMBER",
               "value":1.0,
               "displayValue":"1"
            },
            {  
               "columnId":7742495065958276,
               "type":"TEXT_NUMBER",
               "value":"ABS",
               "displayValue":"ABS"
            },
            {  
               "columnId":2112995531745156,
               "type":"TEXT_NUMBER",
               "value":"ShedsRus",
               "displayValue":"ShedsRus"
            },
            {  
               "columnId":6616595159115652,
               "type":"TEXT_NUMBER",
               "value":"OK",
               "displayValue":"OK"
            }
         ]
      },
      {  
         "id":8503356802000772,
         "rowNumber":2,
         "siblingId":3999757174630276,
         "expanded":true,
         "createdAt":"2015-06-23T11:05:26+10:00",
         "modifiedAt":"2015-06-23T14:17:47+10:00",
         "cells":[  
            {  
               "columnId":3238895438587780,
               "type":"TEXT_NUMBER",
               "value":2.0,
               "displayValue":"2"
            },
            {  
               "columnId":7742495065958276,
               "type":"TEXT_NUMBER",
               "value":"Barns",
               "displayValue":"Barns"
            },
            {  
               "columnId":2112995531745156,
               "type":"TEXT_NUMBER",
               "value":"BarnsRus",
               "displayValue":"BarnsRus"
            },
            {  
               "columnId":6616595159115652,
               "type":"TEXT_NUMBER",
               "value":"OK",
               "displayValue":"OK"
            }
         ]
      }
   ]
}

此字符串看起来包含太多信息,例如id&s;和类型等... 我想要的只是我的列名及其相关数据。 然后,将此信息显示为gridview控件。

这是我的json请求:

        string sURL;
        sURL = "https://api.smartsheet.com/1.1/sheet/4705121406871428";

        WebRequest wrGETURL;
        wrGETURL = WebRequest.Create(sURL);
        wrGETURL.ContentType = "application/json";
        wrGETURL.Method = "GET";
        wrGETURL.Headers["Authorization"] = "Bearer ******************";

        Stream objStream;
        objStream = wrGETURL.GetResponse().GetResponseStream();

        StreamReader objReader = new StreamReader(objStream);

        string jsonResult = objReader.ReadToEnd();

如果有人可以提供一些指导来让我为我工作,我将不胜感激...请原谅我对这个主题的完全无知...在这里完成newb!感谢。

1 个答案:

答案 0 :(得分:0)

在Smartsheet API的响应中,有一个columns对象,它由工作表中的一列列组成。遍历此数组以获取列标题和列ID。

然后,浏览rows对象并遍历每一行。对于每一行,您将要遍历该行的cells数组并输出displayValue。确保单元格的每个columnId与您要打印的列的id匹配,因为如果Smartsheet中有一个空白单元格,Smartsheet API将不会返回任何单元格对象。

您可以找到有关Smartsheet API here的更多信息。而且,如果您还没有这样做,我还建议您查看Smartsheet API的C# SDK