Android Question JSON and quotes

red30

Well-Known Member
Licensed User
Longtime User
I wrote a value containing quotation marks in the map and saved this map in JSON.
B4X:
Sub CreatJSON
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(Map1)
    Dim s As String
    s=JSONGenerator.ToPrettyString(2).Replace("\","")
    File.WriteString(File.DirInternal,"123.json",s)
End Sub
Now when I try to get the json file back to the map, I get the error:
B4X:
                    JSON.Initialize(File.ReadString(File.DirInternal&,"123.json"))
                    Map1=JSON.NextObject
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Class not found: b4a.example3.customlistview, trying: red.ap.expert.customlistview
Error occurred on line: 305 (Main)
org.json.JSONException: Unterminated object at character 35 of {
  "123": false,
  "456": ""test",
  "789": "GGGGHHJKKKJJHGGGH",
  "007": "7"
}
    at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
    at org.json.JSONTokener.readObject(JSONTokener.java:394)
    at org.json.JSONTokener.nextValue(JSONTokener.java:100)
    at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:48)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at red.ap.expert.main._createitem(main.java:1668)
    at red.ap.expert.main._first(main.java:1199)
    at red.ap.expert.main$ResumableSub_Activity_Create.resume(main.java:1012)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:250)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA$2.run(BA.java:370)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5281)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:932)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:748)
    at dalvik.system.NativeStart.main(Native Method)
I need to read json when launching the application, and now it crashes.
How can I fix a json file, if it already has a key-value pair in the value of the quote?
 

DonManfred

Expert
Licensed User
Longtime User
s=JSONGenerator.ToPrettyString(2).Replace("\","")
why are you removing it here? A " inside a String must be Escaped. That´s what the \ does. Removing it is a mistake.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
  
    Log(GetDeviceLayoutValues.ApproximateScreenSize)
    CreatJSON
    If File.Exists(File.DirInternal,"123.json") Then
        Dim json As String = File.ReadString(File.DirInternal,"123.json")
        Log(json)
        Dim parser As JSONParser
        parser.Initialize(json)
        Dim root As Map = parser.NextObject
        Dim A123 As String = root.Get("A123")
        Log(A123)
        Dim A456 As String = root.Get("A456")
        Log(A456)
        Dim A007 As String = root.Get("A007")
        Log(A007)
        Dim A789 As String = root.Get("A789")
        Log(A789)
    End If
  
End Sub
Sub CreatJSON
    Dim m As Map = CreateMap("A123": False, "A456": $""test"$,"A789": "GGGGHHJKKKJJHGGGH","A007": "7")
  
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(m)
    Dim s As String
    s=JSONGenerator.ToPrettyString(2)
    File.WriteString(File.DirInternal,"123.json",s)
End Sub

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
{
"A123": false,
"A456": "\"test",
"A789": "GGGGHHJKKKJJHGGGH",
"A007": "7"
}
false
"test
7
GGGGHHJKKKJJHGGGH
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
why are you removing it here?
This I took from another topic.
B4X:
Dim gen As JSONGenerator
Dim myObj As Map
myObj.Initialize
myObj.Put("dttm","01/03/2017 16:24:34")

gen.Initialize(myObj)
Msgbox(gen.ToString,"TESTING")

The Msgbox show {"dttm":01\/03\/2017 14:24:34"}
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
DonManfred,
Thanks for the answer! And how do I fix it if I already have such a file? Corrupted file fix fail?
I mean, if I already have such a corrupted file with the "quotation mark", won't I restore it?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
That is, it is not how not to recover? Several people had important data there ... Sorry ...
 
Upvote 0

emexes

Expert
Licensed User
That is, it is not how not to recover? Several people had important data there ... Sorry ...
If you are lucky and your JSON is nicely formatted in a consistent style, eg:

B4X:
{
    "name" : "Bill Tell",
    "age" : "24",
    "gender" : "male",
    "town" : "Omsk"
}

then, for that example, you could safely assume that quotation marks that are:

- at the start of the line, and after a "{" line or a line ending with a ","
- at the end the line, or in the quote-comma sequence at the end of the line
- in the quote-space-colon-space-quote sequence: " : "

are real quotation marks, and any other quotation marks are the ones you need to fix by having your program add a \ back in front, before handing the string to the JSON parser.

Presumably the affected quotation marks are only in the values, not in the keys, so that's another filter you could use to find the problem quotes.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…