open-force / jsonparse

Apex JSON parser to make it easier to extract information from nested JSON structures
MIT License
137 stars 30 forks source link

String text in parsing loop conflicts with class variable text #5

Closed IrwinHorowitz closed 3 years ago

IrwinHorowitz commented 3 years ago

the local instance of 'text' was assigned instead of the this.text when 'text' is an entity in the parsed json. Possibly make your String text field unique by naming it j2a_text or something like that or use the this.text in the assignment. Below I did both to resolve the issue. Thanks for this great utility.

public class Fields { public String description {get;set;} public Boolean required {get;set;} public String text {get;set;} public String type {get;set;} public String id {get;set;} public String value {get;set;} public List options {get;set;} public String prompt {get;set;}

    public Fields(JSONParser parser) {

        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String ftext = parser.getText();

                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (ftext == 'description') {
                        description = parser.getText();
                    } else if (ftext == 'required') {
                        required = parser.getBooleanValue();
                    } else if (ftext == 'text') {

                        this.text = parser.getText();

                         System.debug('This Text: '+this.text);
                         System.debug('Other text: '+text);

                    } else if (ftext == 'type') {
                        type = parser.getText();
                    } else if (ftext == 'id') {
                        id = parser.getText();
                    } else if (ftext == 'value') {
                        value = parser.getText();
                    } else if (ftext == 'options') {
                        options = arrayOfOptions(parser);
                    } else if (ftext == 'prompt') {
                        prompt = parser.getText();
grekker commented 3 years ago

Hi @IrwinHorowitz! I was very confused by this issue at first, but then I realized what's going on.

I think you are using the native Apex class called JSONParser and documented here (https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_JsonParser.htm).

This Github repository is for a custom Apex class with a similar name but it is not the first-party class that Salesforce built.

IrwinHorowitz commented 3 years ago

So sorry grekker. This is where I should be https://github.com/superfell/json2apex/issues. They built an apex class builder for parsing json. http://json2apex.herokuapp.com/ . My apologies again. I'll blame google and my not paying enough attention.

Irwin

On Thu, Oct 14, 2021 at 6:27 PM grekker @.***> wrote:

Hi @IrwinHorowitz https://github.com/IrwinHorowitz! I was very confused by this issue at first, but then I realized what's going on.

I think you are using the native Apex class called JSONParser and documented here ( https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_JsonParser.htm ).

This Github repository is for a custom Apex class with a similar name but it is not the first-party class that Salesforce built.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/open-force/jsonparse/issues/5#issuecomment-943786097, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIL2535KPE4ZN6AAQKSTZODUG5KMLANCNFSM5EUBVWEA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

-- Irwin Horowitz Solutions Architect

28 West 23rd Street New York, NY 10010 (845) 803-6996

grekker commented 3 years ago

No worries! While you're here you should check out this library, might come in handy for you sometime :)