zserge / jsmn

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
MIT License
3.72k stars 783 forks source link

xml to Json and parsor #207

Closed embbo closed 3 years ago

embbo commented 3 years ago

Hi all, I convert xml to Json and tring to parse , but my object is not recognizeable to JSMN , what can be the problem .. Here is Json.

static const char *JSON_STRING = "{\n" " \"NamespaceUris\": {\n" " \"Uri\": \"reoag.com\"\n" " },\n" " \"Aliases\": {\n" " \"Alias\": [\n" " {\n" " \"@Alias\": \"Int32\",\n" " \"#text\": \"i=6\"\n" " },\n" " {\n" " \"@Alias\": \"Float\",\n" " \"#text\": \"i=10\"\n" " },\n" " {\n" " \"@Alias\": \"Organizes\",\n" " \"#text\": \"i=35\"\n" " },\n" " {\n" " \"@Alias\": \"HasTypeDefinition\",\n" " \"#text\": \"i=40\"\n" " },\n" " {\n" " \"@Alias\": \"HasComponent\",\n" " \"#text\": \"i=47\"\n" " }\n" " ]\n" " },\n" " \"UAObject\": [\n" " {\n" " \"@NodeId\": \"i=20002\",\n" " \"@BrowseName\": \"0:My_Object\",\n" " \"@ParentNodeId\": \"i=85\",\n" " \"DisplayName\": \"My_Object\",\n" " \"References\": {\n" " \"Reference\": [\n" " {\n" " \"@ReferenceType\": \"Organizes\",\n" " \"@IsForward\": \"false\",\n" " \"#text\": \"i=85\"\n" " },\n" " {\n" " \"@ReferenceType\": \"HasTypeDefinition\",\n" " \"#text\": \"i=58\"\n" " },\n" " {\n" " \"@ReferenceType\": \"HasComponent\",\n" " \"#text\": \"i=20003\"\n" " }\n" " ]\n" " }\n" " },\n" " {\n" " \"@NodeId\": \"i=20003\",\n" " \"@BrowseName\": \"0:reovib1\",\n" " \"@ParentNodeId\": \"i=20002\",\n" " \"DisplayName\": \"reovib1\",\n" " \"References\": {\n" " \"Reference\": [\n" " {\n" " \"@ReferenceType\": \"HasComponent\",\n" " \"@IsForward\": \"false\",\n" " \"#text\": \"i=20002\"\n" " },\n" " {\n" " \"@ReferenceType\": \"HasTypeDefinition\",\n" " \"#text\": \"i=58\"\n" " },\n" " {\n" " \"@ReferenceType\": \"HasComponent\",\n" " \"#text\": \"i=20004\"\n" " },\n" " {\n" " \"@ReferenceType\": \"HasComponent\",\n" " \"#text\": \"i=20005\"\n" " }\n" " ]\n" " }\n" " }\n" " ],\n" " \"UAVariable\": [\n" " {\n" " \"@NodeId\": \"i=20004\",\n" " \"@BrowseName\": \"0:var1\",\n" " \"@ParentNodeId\": \"i=20003\",\n" " \"@DataType\": \"Float\",\n" " \"DisplayName\": \"var1\",\n" " \"Description\": \"var1\",\n" " \"References\": {\n" " \"Reference\": [\n" " {\n" " \"@ReferenceType\": \"HasComponent\",\n" " \"@IsForward\": \"false\",\n" " \"#text\": \"i=20003\"\n" " },\n" " {\n" " \"@ReferenceType\": \"HasTypeDefinition\",\n" " \"#text\": \"i=63\"\n" " }\n" " ]\n" " },\n" " \"Value\": {\n" " \"Float\": \"0.0\"\n" " }\n" " },\n" " {\n" " \"@NodeId\": \"i=20005\",\n" " \"@BrowseName\": \"0:var2\",\n" " \"@ParentNodeId\": \"i=20003\",\n" " \"@DataType\": \"Int32\",\n" " \"DisplayName\": \"var2\",\n" " \"Description\": \"var2\",\n" " \"References\": {\n" " \"Reference\": [\n" " {\n" " \"@ReferenceType\": \"HasComponent\",\n" " \"@IsForward\": \"false\",\n" " \"#text\": \"i=20003\"\n" " },\n" " {\n" " \"@ReferenceType\": \"HasTypeDefinition\",\n" " \"#text\": \"i=63\"\n" " }\n" " ]\n" " },\n" " \"Value\": {\n" " \"Int32\": \"0\"\n" " }\n" " }\n" " ]\n" "}";

pt300 commented 3 years ago

I need you to explain in detail what happens, your configuration and provide the code that you use for it.

embbo commented 3 years ago

@pt300 : I used simple example and replace the My string (above one ) with static const char *JSON_STRING = "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n " "\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}"; .. but I got error--- Failed to parse JSON: -1

pt300 commented 3 years ago

If you take a look at jsmnerr enum you will notice that return value of -1 means that the tokens array is too small. I suggest you increase its size.

embbo commented 3 years ago

@pt300 thank you , I changed it and its solved but how can I extract the value of : BrowseName in UAObject

{ "NamespaceUris": { "Uri": "reoag.com" }, "Aliases": { "Alias": [ { "@Alias": "Int32", "#text": "i=6" }, { "@Alias": "Float", "#text": "i=10" }, { "@Alias": "Organizes", "#text": "i=35" }, { "@Alias": "HasTypeDefinition", "#text": "i=40" }, { "@Alias": "HasComponent", "#text": "i=47" } ] }, "UAObject": [ { "@NodeId": "i=20002", "@BrowseName": "0:My_Object", "@ParentNodeId": "i=85", "DisplayName": "My_Object", "References": { "Reference": [ { "@ReferenceType": "Organizes", "@IsForward": "false", "#text": "i=85" }, { "@ReferenceType": "HasTypeDefinition", "#text": "i=58" }, { "@ReferenceType": "HasComponent", "#text": "i=20003" } ] } }, { "@NodeId": "i=20003", "@BrowseName": "0:reovib1", "@ParentNodeId": "i=20002", "DisplayName": "reovib1", "References": { "Reference": [ { "@ReferenceType": "HasComponent", "@IsForward": "false", "#text": "i=20002" }, { "@ReferenceType": "HasTypeDefinition", "#text": "i=58" }, { "@ReferenceType": "HasComponent", "#text": "i=20004" }, { "@ReferenceType": "HasComponent", "#text": "i=20005" } ] } } ], "UAVariable": [ { "@NodeId": "i=20004", "@BrowseName": "0:var1", "@ParentNodeId": "i=20003", "@DataType": "Float", "DisplayName": "var1", "Description": "var1", "References": { "Reference": [ { "@ReferenceType": "HasComponent", "@IsForward": "false", "#text": "i=20003" }, { "@ReferenceType": "HasTypeDefinition", "#text": "i=63" } ] }, "Value": { "Float": "0.0" } }, { "@NodeId": "i=20005", "@BrowseName": "0:var2", "@ParentNodeId": "i=20003", "@DataType": "Int32", "DisplayName": "var2", "Description": "var2", "References": { "Reference": [ { "@ReferenceType": "HasComponent", "@IsForward": "false", "#text": "i=20003" }, { "@ReferenceType": "HasTypeDefinition", "#text": "i=63" } ] }, "Value": { "Int32": "0" } } ] }