I am consuming the rest post service in my c# application which sends the jsonstring result. I want to convert that result into the list but it is giving error.
Output String:-
{"SearchDocumentResult":"[{\"ContextType\":\"ddd\",\"ContextIDPrimary\":\"TestContextP\",\"ContextIDSecondary\":\"TestContextS\",\"ContextIDTertiary\":\"\",\"RequirementID\":\"17\",\"Source\":\"image\",\"DocumentName\":\"4.pdf\",\"DocumentImage\":\"\",\"ErrorCode\":\"0\",\"ErrorDescription\":\"\",\"createdBy\":\"\",\"createdDate\":\"12-07-2017 15:39:57\"},{\"ContextType\":\"ddd\",\"ContextIDPrimary\":\"TestContextP\",\"ContextIDSecondary\":\"TestContextS\",\"ContextIDTertiary\":\"\",\"RequirementID\":\"18\",\"Source\":\"image\",\"DocumentName\":\"4.pdf\",\"DocumentImage\":\"\",\"ErrorCode\":\"0\",\"ErrorDescription\":\"\",\"createdBy\":\"\",\"createdDate\":\"12-07-2017 15:42:07\"}]"}
I am getting above output as json string . I am unable to convert above string to List. Can anyone guide me on this.
What I have tried:
public class test
{
public string ContextType { get; set; }
public string ContextIDPrimary { get; set; }
public string ContextIDSecondary { get; set; }
public string ContextIDTertiary { get; set; }
public string Source{get;set;}
public string DocumentID { get; set; }
}
public class JsonHelper
{
public static string JsonSerializer<T>(T t)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, t);
string jsonString = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return jsonString;
}
public static T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
}
public class RootObject
{
public IEnumerable<SearchDocumentResult> SearchDocumentResult { get; set; }
}
protected void Button1_Click(object sender, EventArgs e)
{
test t = new test
{
ContextType = "ddd",
ContextIDPrimary = "",
ContextIDSecondary = "",
ContextIDTertiary = " ",
Source = "",
DocumentID = ""
};
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(test));
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, t);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
string result = webClient.UploadString("http://192.168.0.151:222/Service1.svc/SearchDocument", "POST", data);
var result11 = JsonHelper.JsonDeserialize<RootObject1>(result);
}