Can I get JSONObject from Default value of Firebase Remote Config
Asked Answered
S

3

8

I need to fetch JSONObject from default value of Remote config in Firebase. By FirebaseRemoteConfig.getString(), it gets converted to the String, but not in the JSONObject, saying

org.json.JSONException: Unterminated object at character xx

I am attaching default values of remote config below:

  <entry>
    <key>welcome_message</key>
    <value>{"version" :1,"type":"category","name":"CATEGORY"}
    </value>
  </entry>

I am now attaching my Java Code below:

  String welcomeMessage = mFirebaseRemoteConfig.getString(WELCOME_MESSAGE_KEY);

  JSONObject categoryListInJson = new JSONObject(welcomeMessage);

Thanks in advance for the answers.

Samira answered 9/11, 2017 at 10:10 Comment(11)
Print String wc , is it a well-formated json ?Rumor
No, it's not @semakoSamira
Can you paste the content of welcomeMessage ? and also in "org.json.JSONException: Unterminated object at character xx", what is the value of xx ? @vipul-k-patilRumor
I've already done this in the question. String comes as: {version:1,type:category,name:BASE CATEGORY} and the value of "xx" is 36, i.e. the Space Character- " "Samira
looks like xml tag value has json.Ifni
Yes, I need to pass it as json. @IfniSamira
Valid Json need quotes around field name and strings, like this {"version":1,"type":"category","name":"BASE CATEGORY"}. You may have it well formatted in your remote config, but you also have to make sure that the string in your runtime has a correct format. Also, the space may come from here BASE[Here]CATEGORY. So missing "", I guess.Rumor
That's the issue. I am not getting the proper format at runtime from the above stated format. Also, the issue of <SPACE> will be gone if double quotes are available after getString()Samira
I solved it using older gradle version. Check this answer: https://mcmap.net/q/507495/-android-firebase-remoteconfig-getstring-method-is-removing-quotes-from-string-inside-default-xmlLazes
Check this https://mcmap.net/q/507495/-android-firebase-remoteconfig-getstring-method-is-removing-quotes-from-string-inside-default-xmlBobinette
any updates for this thread?Pedropedrotti
A
1

It's bug on Android Studio 3.0 or above.

Alikee answered 1/12, 2017 at 9:2 Comment(1)
You're right. I solved it by using classpath 'com.android.tools.build:gradle:2.3.3' https://mcmap.net/q/507495/-android-firebase-remoteconfig-getstring-method-is-removing-quotes-from-string-inside-default-xmlLazes
T
0

I saw a bug where the quotes from strings were getting stripped when picking values from default.xml

Can you try either of 2 things?

  1. Enable developer mode and set cache expiration to 0. Then setup the JSON on firebase remote config console. Then make a fetch on device. This is because the bug isn't when JSON is fetched from console, it seemed only when pulling value from default xml.
  2. Instead of using a string in default.xml JSON, use a number - and see if that works?

Thanks

Towel answered 18/12, 2017 at 18:24 Comment(0)
P
0

There are multiple solutions for this problem

1. Use firebaseConfig.setDefaults(Map); for setting up the default configuration.

2. Use backslash () before Double Quotes(") in your String in .xml file.

3. Replace

classpath 'com.android.tools.build:gradle:3.0.1' with
classpath 'com.android.tools.build:gradle:2.3.3'
and 
google() with
maven { url 'https://maven.google.com' }

4. Add android.enableAapt2 = false in gradle.properties

Pennoncel answered 22/6, 2018 at 4:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.