M malvin garabha Member Mar 27, 2024 #1 i am get an error when i try to cast a value in a map to a boolean variable . The data is from a JSON string which i parsed into a map and it contains a boolean field. Here is the code snippet which generates the error: B4X: Copy Dim parser As JSONParser Dim JSONCategoriesMap As Map parser.Initialize(JSONstoredAllCategories) JSONCategoriesMap=parser.NextObject Dim categorieslist As List =JSONCategoriesMap.get("categories") For Each field As Map In categorieslist Dim categoryactive As Boolean =field.Get("is_active").As(Boolean) The error from the logs is this one: java.lang.RuntimeException: Cannot parse: 1 as boolean I am new to the B4X language and any help would be appreciated.
i am get an error when i try to cast a value in a map to a boolean variable . The data is from a JSON string which i parsed into a map and it contains a boolean field. Here is the code snippet which generates the error: B4X: Copy Dim parser As JSONParser Dim JSONCategoriesMap As Map parser.Initialize(JSONstoredAllCategories) JSONCategoriesMap=parser.NextObject Dim categorieslist As List =JSONCategoriesMap.get("categories") For Each field As Map In categorieslist Dim categoryactive As Boolean =field.Get("is_active").As(Boolean) The error from the logs is this one: java.lang.RuntimeException: Cannot parse: 1 as boolean I am new to the B4X language and any help would be appreciated.
Daestrum Expert Licensed User Longtime User Mar 27, 2024 #2 You could try something like Code: Copy Dim categoryactive As Boolean = IIF(field.Get("is_active")=1,true,false) A Java Boolean has no numeric value, it's either true or false (unlike other languages which can accept true/false as a numeric value) Last edited: Mar 27, 2024 Upvote 0
You could try something like Code: Copy Dim categoryactive As Boolean = IIF(field.Get("is_active")=1,true,false) A Java Boolean has no numeric value, it's either true or false (unlike other languages which can accept true/false as a numeric value)
M malvin garabha Member Mar 27, 2024 #3 Daestrum said: You could try something like Code: Copy Dim categoryactive As Boolean = IIF(field.Get("is_active")=1,true,false) A Java Boolean has no numeric value, it's either true or false (unlike other languages which can accept true/false as a numeric value) Click to expand... Thanks seems to be the same with the way i had solved it . i had used this : B4X: Copy If field.Get("is_active")=1 Then but i wasn't sure if it was the best way and also i wasn't aware that a java boolean is different from other languages which have 1 or 0 for boolean variables thanks for the information. Upvote 0
Daestrum said: You could try something like Code: Copy Dim categoryactive As Boolean = IIF(field.Get("is_active")=1,true,false) A Java Boolean has no numeric value, it's either true or false (unlike other languages which can accept true/false as a numeric value) Click to expand... Thanks seems to be the same with the way i had solved it . i had used this : B4X: Copy If field.Get("is_active")=1 Then but i wasn't sure if it was the best way and also i wasn't aware that a java boolean is different from other languages which have 1 or 0 for boolean variables thanks for the information.