-2

Please help, I'm stuck with this

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
     var result = streamReader.ReadToEnd();
     Console.WriteLine(result);
}

output :

{
    "request_number":"REQ0010201",
    "request_id":"b1c8ba46db3ffa807ea0f4e9bf9619ca",
    "sys_id":"35c8ba46db3ffa807ea0f4e9bf9619ca"
}

But I want output as only request_id to be printed : b1c8ba46db3ffa807ea0f4e9bf9619ca

EA-Lille
  • 561
  • 7
  • 21
Rahul Davis
  • 31
  • 1
  • 1
  • 4
  • You can refer to this to understand JSON: https://stackoverflow.com/documentation/c%23/9910/getting-started-json-with-c-sharp#t=201707081329500296074 – Nisarg Shah Jul 08 '17 at 13:30
  • Possible duplicate of [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Nisarg Shah Jul 08 '17 at 13:34

4 Answers4

0

if the return data is a json you can two options:

  1. Using substring and indexof in a string for search the data after request_id":"
  2. Serealize the json into a class and use in a class. For this you could use the classes of microsoft or newtonsoft . I prefer this option

see this link

Hope I help you

0

create a class like this:

public class myresultclass
{
    public string request_number {get;set;}
    public string request_id {get;set;}
    public string sys_id {get;set;}
}

then you chango your function so:

using System.Web.Script.Serialization;
using System.IO;

public string mymethod()
{

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
            var result = streamReader.ReadToEnd();

        using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(result)))  
        {  
            // Deserialization from JSON  
            DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(myresultclass));  
        myresultclass obj = (myresultclass)deserializer.ReadObject(ms);  

        }

            Console.WriteLine(result);

        Console.WriteLine(myresultclass.request_id);
    }
}

Note: I write this with notepad if doesn't work get me the error :)

0

You can easy do it via JSON.NET

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
     var result = streamReader.ReadToEnd();
     var jObject = JObject.Parse(result);
     var value = (string)jObject["request_id"];
     Console.WriteLine(value);
}
Ilya Sulimanov
  • 7,636
  • 6
  • 47
  • 68
  • This didn't worked not returning any value . var value = (string)jObject["request_id"]; Console.WriteLine(value); //value is null – Rahul Davis Jul 08 '17 at 14:11
0

if You Are Sure That The Request Id Will Always By The Second Attr in The Json Object Then You Can Do Some String Manipulation string.Split(',') Will Return Array Of Strings Split By The Comma , Now We Know That The Request Id Is The Second Eelment In This Array So string.Split(',')[1] Should return The Following String "request_id":"b1c8ba46db3ffa807ea0f4e9bf9619ca"

if U Chain Another Split But this Time With Colon and Got The Second Member Of The Out Put Array It Will Be The Id Value string.Split(':')[1]

If You Don't Want The Quotation Mark You Can Do String.Replace("\"",string.Empty)

So Your Code Should be JsonObject.Split(',')[1].Split(':')[1].Replace("\"",string.Empty)

You Could Split By The Colon From The First Time But I Want To Explain More

if You Don't Like That You Can Use NewtonSoft.Json Package To Deserialize The Json Object To Previously Created R