0

I had edited my question :

How can i deserialize the JSON string shows below :

"{\"acctId\": \"Test10001\",\"amount\": 200,\"currency\": \"USD\",\"Code\": \"Test\",\"serialNo\": \"1234566789asdsad0\"}"

Please give suggestion how can I get the data by using this method or any other recommended method.

3
  • 1
    expose your full function Commented Oct 20, 2016 at 4:54
  • 1
    res is JSON string. So use of HttpUtility.ParseQueryString(res); is wrong here. Deserialize res using JSON serializer like JSON.NET Commented Oct 20, 2016 at 5:26
  • if that other way of deserialize without using other library ? Commented Oct 20, 2016 at 6:56

4 Answers 4

2

Suggesting you to use StreamWriter as below. Use this function and pass your string and return a the Stream which will give you the desired JSON Content

public static Stream GenerateStreamFromString(string s)
{
         MemoryStream stream = new MemoryStream();
         StreamWriter writer = new StreamWriter(stream);
         writer.Write(s);
         writer.Flush();
         stream.Position = 0;
         return stream;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The payload of your POST request seems to be in JSON format, so you should use a JSON parsing library to parse it, such as Json.NET. Then you would write something like:

JsonConvert.DeserializeObject<YourRequestObject>(res)

Comments

0

I think below code should serve your purpose:

    public class DeserializedData
    {
        public string acctId { get; set; }
        public string amount { get; set; }
        public string currency { get; set; }
        public string Code { get; set; }
        public string serialNo { get; set; }
    }    

StreamReader reader = new StreamReader(streamdata);
string res = reader.ReadToEnd();

Use third party dlls like Json.NET or Restsharp:

1.) Using Json.Net Json.NET

var result = JsonConvert.DeserializeObject<DeserializedData>(res);

2.) Using Restsharp Restsharp

var jsonDeserializer = new RestSharp.Deserializers.JsonDeserializer();
var response = jsonDeserializer.Deserialize<DeserializedData>(res);

Let me know if it doesn't work for you.

3 Comments

Thanks, thats work. You save my time , thanks again =)
No worries, mate!!
@saket is your bug fixed?
0

you can read Json string like this

dynamic stuff = JObject.Parse(res.ToString());

        string acctId= stuff.acctId;

But the response string you are parsing should be json formatted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.