In the code below, GetResults()
is using value of response.Content
from GetAddress()
:
"{\"result\":\"someaddress\",\"error\":null,\"id\":\"rpctest\"}\n"
This is wrong as it should be using new request parameters defined in the method. So I get this error when I run the application:
System.Text.Json.JsonException: 'The JSON value could not be converted to System.Collections.Generic.List`1[Myapp.mainResult]. Path: $.result | LineNumber: 0 | BytePositionInLine: 54.'
I tried adding this in GetResults()
before adding new parameters but it does not work and gives null JSON error:
restParams.request.Parameters.Clear();
c#
namespace Myapp
{
public class mainResult
{
public string address { get; set; }
public string email { get; set; }
public bool remote { get; set; }
}
public class Params
{
public RestClient? client { get; set; }
public RestRequest? request { get; set; }
}
public class Addresses
{
public string? address { get; set; }
}
public class addResponse
{
public string result { get; set; }
public object error { get; set; }
public string id { get; set; }
}
public class mainResponse
{
public List<mainResult> result { get; set; }
public object error { get; set; }
public string id { get; set; }
}
public partial class MainWindow : Window
{
private static Params restParams = new Params();
private static Addresses home_addresses = new Addresses();
private static addResponse addResponse = new addResponse();
private static mainResponse mainResponse = new mainResponse();
public MainWindow()
{
InitializeComponent();
if (ConnectServer())
{
new_address.Content = GetAddress();
SaveAddress();
}
}
public static bool ConnectServer()
{
try
{
restParams.client = new RestClient("http://localhost:8080");
restParams.client.Timeout = -1;
restParams.request = new RestRequest(Method.POST);
restParams.request.AddHeader("Authorization", "Basic ZDhjZGUyNDIzNDhhNmQwNTIwZGI1ZjcyNjk");
restParams.request.AddHeader("Content-Type", "application/json");
}
catch (Exception ex)
{
Console.Write(ex.ToString());
return false;
}
return true;
}
public static string GetAddress()
{
const string? body = @"{""jsonrpc"": ""1.0"", ""id"": ""rpctest"", ""method"": ""getaddress"", ""params"": [""test""]}";
restParams.request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = restParams.client.Execute(restParams.request);
addResponse newaddress = JsonSerializer.Deserialize<addResponse>(response.Content);
home_addresses.address = newaddress.result;
return home_addresses.address;
}
public static async void SaveAddress()
{
string address = home_addresses.address;
string json_path = @"E:\address_test.json";
if (!File.Exists(json_path))
{
await using (StreamWriter writer = File.CreateText(json_path))
{
writer.WriteLine(address);
}
}
else
{
await using (StreamWriter writer = File.AppendText(json_path))
{
writer.WriteLine(address);
}
}
}
public static List<mainResult> GetResults()
{
const string? body = @"{""jsonrpc"": ""1.0"", ""id"": ""rpctest"", ""method"": ""getresults"", ""params"": []}";
restParams.request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = restParams.client.Execute(restParams.request);
mainResponse main_response = JsonSerializer.Deserialize<mainResponse>(response.Content);
mainResponse.result = main_response.result;
return mainResponse.result;
}
private void getresults_button_Click(object sender, RoutedEventArgs e)
{
mylist.AppendText(GetResults().ToString());
}
}
}
What is wrong with the code and how do I fix this?