我可以向 Firebase 远程配置声明一个数组吗?
Can I declare an array to Firebase Remote config?
我是 Android 和 Firebase 的新手。
是否可以在 Firebase Remote Config 的参数键内声明一个数组?
我想为某些特定的 models/mobile 设备提供一些促销活动。因此,如果我可以声明一系列型号(即三星 J5、小米 Note2 等),我可以轻松地在这些型号上启用促销。
请帮助我。
Firebase Remote Config 中的所有值最终都存储为字符串。布尔值、数字等都归结为字符串。当您请求其他类型时,SDK 只会解析该字符串值。
远程配置中没有 "native" 个数组。如果你想在 Remote Config 参数中使用一系列有序的值,你应该以一种可以从字符串值中解析的方式来表示它(例如 JSON 或一些简单的分隔字符串)。
与 答案相同,但有代码。
这在 Swift 中,但您仍然应该有所了解。
对于数组,使用分隔字符串,例如
"iron,wood,gold"
并使用 .components(separatedBy: ",")
将其拆分为字符串数组
对于字典,使用 "doubly" 分隔字符串,例如
"iron:50, wood:100, gold:2000"
使用
将其转换为字典
var actualDictionary = [String: Int]()
// Don't forget the space after the comma
dictionaryString.components(separatedBy: ", ").forEach({ (entry) in
let keyValueArray = entry.components(separatedBy: ":")
return actualDictionary[keyValueArray[0]] = Int(keyValueArray[1])
})
return actualDictionary
我还推荐 JSON - 因为您可以在解析 JSON 时进行某种错误检测。通过拆分,您永远不会知道这是否会生成正确的数组。
远程配置最近添加了通过将键值列表保存为 JSON 格式来保存键值列表的选项。
示例用法:
1.Json 存储在远程配置中:
[
{
"lesson": "1",
"versionCode": 2
},
{
"lesson": "2",
"versionCode": 4
},
{
"lesson": "3",
"versionCode": 1
}
]
2.Kotlin 型号
data class Lesson(val lesson: Int, val versionCode: Int)
3.Retrieve json
String object = FirebaseRemoteConfig.getInstance().getString("test_json");
Gson gson = new GsonBuilder().create();
List<Lesson> lessons = gson.fromJson(object, new TypeToken<List<Lesson>>(){}.getType());
我喜欢 N Kuria's 的答案,但这需要您 json 中的空格是准确的。如果您想要更容错的东西,这里是通过 JSONSerialization 读取整数数组的代码。
假设您的 json:
中有这个整数数组
{
"array": [1, 5, 4, 3]
}
func getArray() -> [Int] {
let jsonString = "{\"array\": [1, 5, 4, 3]}"
let array = [Int]()
guard let data = jsonString.data(using: .utf8) else { return array }
if let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) {
guard let theArray = json as? [String: [Int]] else { return array }
return theArray.first?.value ?? array
// If you were reading in objects instead of ints, you would do
// for (key, value) in theArray { /* append values to array */ }
}
return array
}
或者,如果您正在读取 class 类型的值,您可以只枚举字典并在每次迭代时附加到数组。 (请参阅第一个 return 下方的评论。)
Firebase 将数组存储为以整数作为键名的对象。如果您使用数据类型 JSON 并使用具有顺序键的对象,您可以获得从远程配置返回的数组。例如...
默认值:
{
"0": "A",
"1": "B",
"2": "C",
"3": "D",
}
返回为 ["A","B","C","D"]
注意它们必须是顺序键! 所以如果值为:
{
"0": "A",
"2": "B",
"3": "C",
"4": "D",
}
它将返回为 ["A", undefined, "B", "C","D"]
您可以在此处阅读有关 Firebase 中的数组的更多信息 => https://firebase.blog/posts/2014/04/best-practices-arrays-in-firebase
我是 Android 和 Firebase 的新手。
是否可以在 Firebase Remote Config 的参数键内声明一个数组?
我想为某些特定的 models/mobile 设备提供一些促销活动。因此,如果我可以声明一系列型号(即三星 J5、小米 Note2 等),我可以轻松地在这些型号上启用促销。 请帮助我。
Firebase Remote Config 中的所有值最终都存储为字符串。布尔值、数字等都归结为字符串。当您请求其他类型时,SDK 只会解析该字符串值。
远程配置中没有 "native" 个数组。如果你想在 Remote Config 参数中使用一系列有序的值,你应该以一种可以从字符串值中解析的方式来表示它(例如 JSON 或一些简单的分隔字符串)。
与
这在 Swift 中,但您仍然应该有所了解。
对于数组,使用分隔字符串,例如
"iron,wood,gold"
并使用 .components(separatedBy: ",")
对于字典,使用 "doubly" 分隔字符串,例如
"iron:50, wood:100, gold:2000"
使用
将其转换为字典 var actualDictionary = [String: Int]()
// Don't forget the space after the comma
dictionaryString.components(separatedBy: ", ").forEach({ (entry) in
let keyValueArray = entry.components(separatedBy: ":")
return actualDictionary[keyValueArray[0]] = Int(keyValueArray[1])
})
return actualDictionary
我还推荐 JSON - 因为您可以在解析 JSON 时进行某种错误检测。通过拆分,您永远不会知道这是否会生成正确的数组。
远程配置最近添加了通过将键值列表保存为 JSON 格式来保存键值列表的选项。
示例用法:
1.Json 存储在远程配置中:
[
{
"lesson": "1",
"versionCode": 2
},
{
"lesson": "2",
"versionCode": 4
},
{
"lesson": "3",
"versionCode": 1
}
]
2.Kotlin 型号
data class Lesson(val lesson: Int, val versionCode: Int)
3.Retrieve json
String object = FirebaseRemoteConfig.getInstance().getString("test_json");
Gson gson = new GsonBuilder().create();
List<Lesson> lessons = gson.fromJson(object, new TypeToken<List<Lesson>>(){}.getType());
我喜欢 N Kuria's 的答案,但这需要您 json 中的空格是准确的。如果您想要更容错的东西,这里是通过 JSONSerialization 读取整数数组的代码。
假设您的 json:
中有这个整数数组{ "array": [1, 5, 4, 3] }
func getArray() -> [Int] {
let jsonString = "{\"array\": [1, 5, 4, 3]}"
let array = [Int]()
guard let data = jsonString.data(using: .utf8) else { return array }
if let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) {
guard let theArray = json as? [String: [Int]] else { return array }
return theArray.first?.value ?? array
// If you were reading in objects instead of ints, you would do
// for (key, value) in theArray { /* append values to array */ }
}
return array
}
或者,如果您正在读取 class 类型的值,您可以只枚举字典并在每次迭代时附加到数组。 (请参阅第一个 return 下方的评论。)
Firebase 将数组存储为以整数作为键名的对象。如果您使用数据类型 JSON 并使用具有顺序键的对象,您可以获得从远程配置返回的数组。例如...
默认值:
{
"0": "A",
"1": "B",
"2": "C",
"3": "D",
}
返回为 ["A","B","C","D"]
注意它们必须是顺序键! 所以如果值为:
{
"0": "A",
"2": "B",
"3": "C",
"4": "D",
}
它将返回为 ["A", undefined, "B", "C","D"]
您可以在此处阅读有关 Firebase 中的数组的更多信息 => https://firebase.blog/posts/2014/04/best-practices-arrays-in-firebase