我需要对此URL发出多个HTTP请求,并使用JArray.Parse进行反序列化。前两次我调用HTTP方法时,它可以正常工作并且可以很好地进行解析,并且可以获取数据,但是第三次调用该方法时,它将引发异常。
我提供了我已使用的代码。有人可以告诉我怎么回事吗?
public static string GETActionInfo(int action_input)
{
string getDevicesURL = "someURL";
string action_id = "", device_id = "",strname="";
int value=0;
JArray parsedArray=null;
HttpsClientResponse response=null;
try
{
response = crestronGetHttp(getDevicesURL); //Makes the HTTP call
if (response.Code != 200)
{
CrestronConsole.PrintLine(response.Code);
return response.Code.ToString();
}
}
catch(Exception e)
{
CrestronConsole.PrintLine("crestronGetHttp()-->"+e.Message);
}
string jsonData="";
using (Stream stream = response.ContentStream)
{
using (StreamReader reader = new StreamReader(stream))
{
try
{
jsonData = reader.ReadToEnd();
}
catch (Exception e)
{
CrestronConsole.PrintLine("ReadToEnd Exception" + e.Message + "_" + e.StackTrace);
}
string receivedData = jsonData;
try
{
parsedArray = JArray.Parse(receivedData);
}
catch (Exception e)
{
CrestronConsole.PrintLine("JArray Parsing
Exception" + e.Message + " " + e.StackTrace);
}
CrestronConsole.PrintLine("Parsed Array" + parsedArray);
try
{
foreach (JObject ob in parsedArray.Children<JObject>())
{
var name = (string)ob["name"];
if (name == "My ecobee")
{
device_id = (string)ob["_id"];
var json = (JArray)ob["actions"];
}
}
}
catch (Exception e)
{
CrestronConsole.PrintLine(" For block--->" + e.Message + "-----" + e.StackTrace);
}
}
}
}
有趣的是,当我添加一条print语句以打印已解析的数组内容并调用此方法时,它工作得很好。但是当我注释掉打印语句时,它在第三次调用时失败。我当时在想,这可能与延迟有关(因为打印经过解析的数组的所有内容需要一些时间)。我尝试添加一个睡眠语句以将其检出,但效果不佳。
它抛出的异常是
JArray Parsing Exception-->Unterminated string. Expected
delimiter: ". Line 1, position 34779.______ at
Newtonsoft.Json.JsonTextReader.ReadStringIntoBuffer(Char quote)
at Newtonsoft.Json.JsonTextReader.ParseProperty(Char firstChar)
at Newtonsoft.Json.JsonTextReader.ParseObject(Char currentChar)
at Newtonsoft.Json.JsonTextReader.ReadInternal()
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Linq.JContainer.ReadContentFrom(JsonReader r)
at Newtonsoft.Json.Linq.JArray.Load(JsonReader reader)
at Newtonsoft.Json.Linq.JArray.Parse(String json)
at yonomi_one_test_one.ControlSystem.GETActionInfo(Int32 action_input)
at yonomi_one_test_one.ControlSystem.ecoDec(String args)
at Crestron.SimplSharpProInternal.SimplSharpProManager.f()
For block--->NullReferenceException----- at yonomi_one_test_one.ControlSystem.GETActionInfo(Int32 action_input)
at yonomi_one_test_one.ControlSystem.ecoDec(String args)