root777

How to check NewtonJson Jarray null/empty error when request

Dec 26th, 2019
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.42 KB | None | 0 0
  1. I'm trying to display the simple element of the array or value, below is my JSON string(an API from python pandas)
  2.  
  3. ```JSON
  4. {
  5.    "msg": "success",
  6.    "state": 10000,
  7.    "data": {
  8.        "data": [
  9.            {
  10.                "index": 0,
  11.                "test_1": 110,
  12.                "test_2": "000001",
  13.                "test_3": "CN",
  14.                "test_4": "Bank",
  15.                "test_5": 893,
  16.                "test_6": 229
  17.            }
  18.        ],
  19.        "schema": {
  20.            "fields": [
  21.                {
  22.                    "type": "integer",
  23.                    "name": "index"
  24.                },
  25.                {
  26.                    "type": "string",
  27.                    "name": "test_1"
  28.                },
  29.                {
  30.                    "type": "string",
  31.                    "name": "test_2"
  32.                },
  33.                {
  34.                    "type": "number",
  35.                    "name": "test_3"
  36.                },
  37.                {
  38.                    "type": "number",
  39.                    "name": "test_4"
  40.                },
  41.                {
  42.                    "type": "number",
  43.                    "name": "test_5"
  44.                },
  45.                {
  46.                    "type": "string",
  47.                    "name": "test_6"
  48.                }
  49.            ],
  50.            "pandas_version": "0.20.0",
  51.            "primaryKey": [
  52.                "index"
  53.            ]
  54.        }
  55.    }
  56. }
  57. ```
  58.  
  59.  
  60.  
  61. Below code of `C#` is the query that I'm using, `TestDataset.cs`:
  62.  
  63. ```C#
  64. using System;
  65. using System.IO;
  66. using System.Net;
  67. using Newtonsoft.Json.Linq;
  68. using ExcelDna.Integration;
  69. using Excel = Microsoft.Office.Interop.Excel;
  70. namespace Test_Atune
  71. {
  72.     public class Request
  73.     {
  74.         Excel.Application ExcelApp = (Excel.Application)ExcelDnaUtil.Application;
  75.         public object GetDatasetFromUrl(string root, string path, string headval, Excel.Range excelAddress)
  76.         {
  77.             string historicalData= "{}";
  78.             var webConnection = new WebClient();
  79.             webConnection.Headers.Add("Host", headval);
  80.             try
  81.             {
  82.                 historicalData = webConnection.DownloadString(root+path);
  83.             }
  84.             catch (WebException ex)
  85.             {
  86.                 string error_str = ex.Message;
  87.                 if (ex.Status.ToString() == "ConnectFailure" || ex.Status.ToString() == "Timeout")
  88.                 {
  89.                     domain = Constant.setURL_ROOT_COMMON(domain);
  90.                     try
  91.                     {
  92.                         historicalData= webConnection.DownloadString(root + path);
  93.                     }
  94.                     catch (WebException ex2)
  95.                     {
  96.                         return "";
  97.                     }
  98.                 }
  99.             }
  100.             finally
  101.             {
  102.                 webConnection.Dispose();
  103.             }
  104.  
  105.             JObject jsonFeed = JObject.Parse(historicalData);
  106.             Excel.Range range = excelAddress;
  107.             JObject B = new JObject();
  108.             JArray data = (JArray)jsonFeed["data"]["data"];
  109.             JArray columns = (JArray)jsonFeed["data"]["schema"]["fields"];
  110.             int rowNum = data.Count;
  111.             int colNum = columns.Count;
  112.             Excel.Range range_head = ExcelApp.Cells[range.Row + 1, range.Column];
  113.             range_head = range_head.get_Resize(1, colNum);
  114.             Excel.Range range_data = ExcelApp.Cells[range.Row + 2, range.Column];
  115.             range_data = range_data.get_Resize(rowNum, colNum);
  116.             // write header
  117.             object[,] headerData = new object[1, colNum];
  118.             for (int iCol = 0; iCol < colNum; iCol++)
  119.             {
  120.                 headerData[0, iCol] = columns[iCol]["name"];
  121.             }
  122.             range_head.Value2 = headerData;
  123.  
  124.             // write data
  125.             object[,] cellData = new object[rowNum, colNum];
  126.             int iRow = 0;
  127.             foreach (JObject jo in data)
  128.             {
  129.                 var a = jo["fnd_cde"];
  130.                 for (int iCol = 0; iCol < colNum; iCol++)
  131.                 {
  132.                     if (columns[iCol]["type"].ToString() == "string")
  133.                     {
  134.                         cellData[iRow, iCol] = "'" + jo[columns[iCol]["name"].ToString()];
  135.                     }
  136.                     else
  137.                     {
  138.                         cellData[iRow, iCol] = jo[columns[iCol]["name"].ToString()];
  139.                     }
  140.                 }
  141.                 iRow += 1;
  142.             }
  143.             range_data.Value2 = cellData;
  144.             return "Total" + rowNum.ToString() + "cells";
  145.         }
  146.     }
  147.  
  148. }
  149. ```
  150.  
  151. And below is the code of `request.cs`
  152.  
  153. ```C#
  154. using ExcelDna.Integration;
  155. using Excel = Microsoft.Office.Interop.Excel;
  156. namespace Test_Atune
  157. {
  158.     public class Dataset
  159.     {
  160.         public static string baseurl = Constant.URL_ROOT_COMMON;
  161.         public static string headval = Constant.HEADVAL_COMMON;
  162.         public static Request request = new Request();
  163.  
  164.         [ExcelFunction(Category = "test", IsMacroType = true, Description = "test dataset")]
  165.         public static object TEST_DATASET(
  166.                     [ExcelArgument(Description = "test_code")] string test_1,
  167.                     [ExcelArgument(Description = "YYYYMMDD")] string test_2,
  168.                     [ExcelArgument(Description = "YYYYMMDD")] string test_3
  169.                     )
  170.         {
  171.             string parstr = @"/test_dataset/?" +
  172.                 @"test_1=" + test_1 +
  173.                 @"&test_2=" + test_2 +
  174.                 @"&test_3=" + test_3;
  175.             ExcelReference caller = (ExcelReference)XlCall.Excel(XlCall.xlfCaller);
  176.             Excel.Range rg = caller.ToPiaRange();
  177.             return ExcelAsyncUtil.Run("TEST_DATASET",
  178.                 new object[] { parstr },
  179.                 () => request.GetDatasetFromUrl(Constant.URL_ROOT_COMMON, parstr, headval, rg));
  180.         }
  181.     }
  182.  
  183. }
  184. ```
  185.  
  186. And I got the following error on `JArray columns = (JArray)jsonFeed["data"]["schema"]["fields"];`
  187.  
  188.  
  189. ```error
  190. System.NullReferenceException HResult=0x80004003  message=Object reference not set to an instance of an object.
  191. ```
  192.  
  193. I tried to debug and I got below results,
  194. ```Name: historicalData, Value:"{}"; Name: jsonFeed,Vaule:Null; Name:B,Vaule:{{}}, Name:data, Value:"null"```
  195.  
  196. I am very new to `C#`, is that an array,domain or url problem, or anything else? How can I do that? thank you so much for any advice.
Advertisement
Add Comment
Please, Sign In to add comment