I am getting a string as a response from my command line. I want to convert it into a json string , which i will later use to convert to a c# object.
The string Response(sub variable has this string as value)
Access Token 00D0E00000019dU!
Alias accp
Client Id SalesforceDevelopmentExperience
Connected Status Connected
Id 00D
Instance Url https://my.salesforce.com
Username ankur
tried converting it into json by below code
string[] subArray = sub.Split('\n');
string output = JsonConvert.SerializeObject(subArray);
var result = JsonConvert.DeserializeObject<Token>(output);
Token Class
public class Token
{
public string AccessToken { get; set; }
public string Alias { get; set; }
}
It Gives this error
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Token' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1
.
Converted JSON
["Access Token 00D0E00000019dU!AQU","Alias accp","Client Id SalesforceDevelopmentExperience","Connected Status Connected","Id 00D","Instance Url https://my.salesforce.com","Username ankur"]
Any Help to convert the string into a JSON/C# object?
subthe above string? you'll have to parse it further?.Spliting on new lines - have you tried parsing it differently?