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

Jay
  • 524
  • 1
  • 7
  • 21

1 Answers1

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.

Rob
  • 11,492
  • 14
  • 59
  • 94
  • 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 Jan 21 '21 at 06:51
  • @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... – Jon Skeet Jan 21 '21 at 09:53
  • 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 Jan 21 '21 at 09:57
  • @Jay: I'm afraid I don't really understand your comment - it would really help if you could edit the question to show a [mcve]. – Jon Skeet Jan 21 '21 at 10:12
  • @Jay: That''s a *long* way from a [mcve]. 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 https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ – Jon Skeet Jan 21 '21 at 11:13