0

I am calling an api where result is coming this way {{'orderNo':123456}}

the same I am trying to access through these codes lines and But it is not working:

using System.Text.Json.JsonDocument doc = System.Text.Json.JsonDocument.Parse(payment["notes"]);

What I feel it should be very easy to access. Well, I just don't want to create any DTO for the same.

FYI, I can achieve this using DTO class like this:

RzOrderDto orderNo = System.Text.Json.JsonSerializer.Deserialize<RzOrderDto>(payment["notes"].ToString(),null);

public class RzOrderDto {
        public string orderno { get; set; }
    }

Now, here I don't want to use any object class but just want value of orderNo

2
  • "It's not working" is pretty vague. Please tell us what happens. Commented Jan 21, 2021 at 6:21
  • maybe you would help a lot if you say what are you trying to achieve with your code. Commented Jan 21, 2021 at 6:23

1 Answer 1

1

{{'orderNo':123456}} is not a valid Json document, you should try to change the data that is coming from the API. If you cannot do that, you need to get rid of the outer pair of braces before parsing the string into an object.

Sign up to request clarification or add additional context in comments.

5 Comments

Well, I can control few things from the API, if I put 'notes': orderno, like this I get result like this: {[ "0", "2", "2", "5", "1", "8", "3", "3", "7", "3" ]}
@Jay: That's still not valid JSON as far as I'm aware. A JSON "object" can't directly contain other objects or arrays - it can only contain properties mapping names to objects/arrays/strings etc. Are you certain that's the exact format of the JSON you get back? If so, I'd talk to the API provider...
thanks @JonSkeet By the time, I am able to get proper json JsonElement root = doc.RootElement; root is printing value as ValueKind = Object : "{ "orderno": "7865752587" }" but I am not able to access value of property orderno
@Jay: I'm afraid I don't really understand your comment - it would really help if you could edit the question to show a minimal reproducible example.
@Jay: That''s a long way from a minimal reproducible example. If I copy and paste that code into a new project, will it allow me to compile, run, and see the problem? Absolutely not. Please read codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question

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.