Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element - workaround if it doesn't find the element ? #5

Open
wattzor opened this issue Aug 5, 2024 · 1 comment
Open

Element - workaround if it doesn't find the element ? #5

wattzor opened this issue Aug 5, 2024 · 1 comment

Comments

@wattzor
Copy link

wattzor commented Aug 5, 2024

I've been working with this JSON parser and have found it to be impressively fast. However, I have encountered a challenge for which I am seeking a solution.

Consider the following example:
json.Decode('{ "integer1": 5, "string1": "la", "null1": null, "mixedNestedArray": [{"name": "Nested object"}, 4, true, [null, null, 5] ], "object1" : { "subint1":5, "substring1":"lala", "subfloat1":5.44 }, "string2": "asdf", "array2" : [5,4] }');

When I retrieve an existing element, such as:
testSTR := json.Element('integer1').AsString();
this works perfectly. However, if I attempt to retrieve an element that does not exist:
testSTR := json.Element('integer123').AsString();

the PLC locks down and throws an exception.

Is there a method to check for the presence of an element before attempting to retrieve it, or alternatively, return an empty string if the element is not found? This functionality is crucial as the data from HTTP requests may vary in structure.

@vChavezB
Copy link

I did something similar as this in Codesys

METHOD Parse : ReplyITF
VAR_INPUT
	pMsg:POINTER TO STRING;
END_VAR
VAR
	decoder:JsonDecoder;
	element: IJSONElement;
	val:STRING;
	encoder:JsonDecoder;
END_VAR

decoder.Decode(pMsg^);
element:=decoder.Element('cid');
IF element = 0 THEN
	// ELEMENT NOT FOUND!!!!!!!
END_IF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants