0

我对 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 和类型等...我想要的只是我的列名及其相关数据。然后,将此信息显示为 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();

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

4

1 回答 1

0

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

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

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

于 2015-06-23T18:05:42.227 回答