简体   繁体   English

Android - 将字符串转换为列表以推送到 Firebase 实时数据库

[英]Android - Converting string to list to push to firebase realtime database

Is there any way to convert the string below to a list?有没有办法将下面的字符串转换为列表? This string is retrieved after scanning a QR code.扫描二维码后检索此字符串。

CashRequest{
    orderid='0',
    user_id='nvHt2U5RnqUwXB4ZK37Zn1DXPV82',
    userName='username',
    userEmail='whateveremailthisis@email.blabla',
    fullName='full name',
    phoneNumber=0,
    totalCash='$304.00',
    totalRV='$34.00', 

    foods=[
        Order{
            userID='nvHt2U5RnqUwXB4ZK37Zn1DXPV82',
            ProductID='-LMDiT7klgoXU8bQEM-4',
            ProductName='Coke',
            Quantity='4',
            Price='1',
            RedemptionPrice='10',
            RedemptionValue='1'},
        Order{
            userID='nvHt2U5RnqUwXB4ZK37Zn1DXPV82',
            ProductID='1000',
            ProductName='Kunau Ring Ring Pradu',
            Quantity='3',
            Price='100',
            RedemptionPrice='10',
            RedemptionValue='10'
        }
    ]
}

The desired output is to store it in firebase realtime database as below :所需的输出是将其存储在 firebase 实时数据库中,如下所示:

在此处输入图片说明

Well you have a few options.那么你有几个选择。 Since it is newline between values, you could use simple newline reads and compare if it starts with "reserved word that you are looking for" then substring from there, but that can get messy and a lot of bloat code.由于它是值之间的换行符,您可以使用简单的换行符读取并比较它是否以“您正在寻找的保留字”开头,然后是从那里开始的子字符串,但这可能会变得混乱和大量膨胀代码。

The simplest way would be to do the known replace first.最简单的方法是先进行已知的替换。 Make a method that replaces all bad json keys with quote surrounded json keys like:制作一个方法,用引号包围的 json 键替换所有坏的 json 键,例如:

 val myJsonCorrected = yourStringAbove.replace("Order", "\"Order"\")

repeat for all known entities until you have made it into valid json.对所有已知实体重复,直到将其转换为有效的 json。 Single ticks are fine for the values, but the keys need quotes as well.单个刻度对值来说很好,但键也需要引号。

Then simply create an object that matches the json format.然后简单地创建一个与 json 格式匹配的对象。

class CashRequestModel{
      @SerializableName("orderid")
      var orderID: Int? = null
      etc.....
      @SerializableName("foods")
      var myFoods: ArrayList<OrderModel>? = null
}

class OrderMode {
     @SerializableName("userID")
     var userID: String? = null
     @SerializableName("ProductID")
     var userID: String? = null
     etc..
}

Then simply convert it to JSON然后只需将其转换为 JSON

val cashRequest = getGson().fromJson(cleanedUpJson, classTypeForCashRequest);

and your done.和你的完成。 Now just use the list.现在只需使用列表。 Of course it would be better if you could get valid JSON without having to clean it up first, but it looks like the keys are known and you can easily code string replaces to fix the bad json before casting it to object that matches the structure.当然,如果您无需先清理即可获得有效的 JSON 会更好,但看起来键是已知的,并且您可以轻松地编写字符串替换代码以修复错误的 json,然后再将其转换为与结构匹配的对象。

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM