Question Error in method JsonDocument.Parse

Vladr

New member
Joined
Mar 28, 2021
Messages
4
Programming Experience
3-5
Please tell me. I can't solve the error in the JsonDocument.Parse method in any way.
Translation of the error text: Error CS1061 / "object" does not contain a definition for "Parse" and it was not possible to find an extension method "Parse" that takes the type "object" as the first argument (possibly missing a Using directive or an assembly reference).
Thanks in advance!

Code:
using Leaf.xNet;
using System;
using System.IO;
using System.Threading;
using System.Text.Json;

namespace ConsoleApplication1
{
    class Checker
    {
        static object fileLocker = new object();

        public static object JsonDocument { get; private set; }

        static string getWumpCode(string res)
        {
            JsonElement json = JsonDocument.Parse(res).RootElement;
            JsonElement store_listing = json.GetProperty("store_listing");
            JsonElement sku = store_listing.GetProperty("sku");

            JsonElement code_property = json.GetProperty("code");
            JsonElement redeemed_property = json.GetProperty("redeemed");
            JsonElement name_property = sku.GetProperty("name");

            bool redeemed = redeemed_property.GetBoolean();

            string redeemed_info = redeemed ? "(Redeemed)" : "";
            string code = code_property.GetString();
            string name = name_property.GetString();

            return $"{code} - {name} {redeemed_info}";
        }

    }
}
 

Attachments

  • screenshot.PNG
    screenshot.PNG
    76.3 KB · Views: 36
Last edited:
Solution
Why is your JsonDocument property declared as type object? There is no Object.Parse method at all, so you obviously should be declaring that as a different type. I'm guessing the type should be JsonDocument. Once you've done that, at least Parse becomes a valid member but, as you have already been told, there is no Parse method with only one parameter. You have to call a method that actually exists. Read the documentation here and decide which overload you want to call and then call it.
Don't post pictures of code. Code is text so post it as text, formatted as code. If we want to test your code for ourselves, we should be able to copy and paste it, not have to rewrite it for ourselves.
 
In the future, please post your code in code tags, not as a screenshot. It makes it very hard to read these screenshots on small devices.

Anyway, the error is accurate. There is no variant of Parse() that just a single string parameter. You need to use one of supported variants.
 
Don't post pictures of code. Code is text so post it as text, formatted as code. If we want to test your code for ourselves, we should be able to copy and paste it, not have to rewrite it for ourselves.
Sorry, I'm not used to asking questions on the forums. I replaced the screenshot with the code
 
In the future, please post your code in code tags, not as a screenshot. It makes it very hard to read these screenshots on small devices.

Anyway, the error is accurate. There is no variant of Parse() that just a single string parameter. You need to use one of supported variants.
Sorry, I didn't understand a little what you wrote. English is not my native language.
 
Why is your JsonDocument property declared as type object? There is no Object.Parse method at all, so you obviously should be declaring that as a different type. I'm guessing the type should be JsonDocument. Once you've done that, at least Parse becomes a valid member but, as you have already been told, there is no Parse method with only one parameter. You have to call a method that actually exists. Read the documentation here and decide which overload you want to call and then call it.
 
Solution
Why is your JsonDocument property declared as type object? There is no Object.Parse method at all, so you obviously should be declaring that as a different type. I'm guessing the type should be JsonDocument. Once you've done that, at least Parse becomes a valid member but, as you have already been told, there is no Parse method with only one parameter. You have to call a method that actually exists. Read the documentation here and decide which overload you want to call and then call it.
Thanks a lot! I was able to solve this problem!
 

Latest posts

Back
Top Bottom