亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使 json 對 recyclerview 有效

無法使 json 對 recyclerview 有效

忽然笑 2023-07-19 15:37:00
我正在嘗試使用 Gson 和 recyclerview 進行 JSON 處理。我的 JSON 不完全有效。在我的 JSON 中,food 字段只有一個字符串被引用,而另一個則不被引用。請參閱下面我的 JSON...[    {"quantity" = 2,     "price" = 15,     "food" = "Fried" Rice},     {"quantity" = 2,    "price" = 20,    "food" = "Rice" and Stew}    ]您可以看到 Fried 有引號,而 Rice 沒有引號,同樣,Rice 和 Stew 也有同樣的情況。最初是這樣的……[    {quantity = 2,     price = 15,     food = Fried Rice},     {quantity = 2,    price = 20,    food = Rice and Stew}    ]我的活動類代碼...Bundle extras = getIntent().getExtras();    if (extras != null) {        String listOfFood = extras.getString("foods");        listOfFood = listOfFood.replaceAll("([\\w]+)[ ]*=", "\"$1\" ="); // to quote before = value        listOfFood = listOfFood.replaceAll("=[ ]*([\\w@\\.]+)", "= \"$1\""); // to quote after = value, add special character as needed to the exclusion list in regex        listOfFood = listOfFood.replaceAll("=[ ]*\"([\\d]+)\"", "= $1"); // to un-quote decimal value        listOfFood = listOfFood.replaceAll("\"true\"", "true"); // to un-quote boolean        listOfFood = listOfFood.replaceAll("\"false\"", "false"); // to un-quote boolean        Log.d(TAG, "onCreate: "+listOfFood);        GsonBuilder builder = new GsonBuilder();        Gson mGson = builder.create();        List<FoodOrder> posts = new ArrayList<FoodOrder>();        posts = Arrays.asList(mGson.fromJson(listOfFood, FoodOrder[].class));        adapter = new RecyclerViewAdapter(FoodsOrderedActivity.this, posts);        recyclerView.setAdapter(adapter);    }我需要將炒飯放在引號之間的食品字段,作為米飯和燉菜的引號,或者是否有解決方法,我想知道。
查看完整描述

3 回答

?
九州編程

TA貢獻1785條經驗 獲得超4個贊

根據您的代碼。(嘗試轉換listOfFood為 JSON )我修改了 2 行代碼,如下所示

        listOfFood = listOfFood.replaceAll("(\\s*)([^{,\\s]+)(\\s*)=","$1\"$2\"$3:"); // to quote before = value and replace = by :
        listOfFood = listOfFood.replaceAll("(:\\s*)([^\\s,{}](\\s*[^\\s,{}]+)*)", "$1\"$2\""); // to quote after = value (= now became :)


查看完整回答
反對 回復 2023-07-19
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

json 結構應該是這樣的:

[{"數量" : 2, "價格" : 15, "食物" : "炒飯"}, {"數量" : 2, "價格" : 20, "食物" : "米飯和燉菜"}]


查看完整回答
反對 回復 2023-07-19
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

json 結構應如下所示 [ { "Key1" : "value1", "Key2" : "value2", }, { "Key1" : "value1", "Key2" : "value2", }



查看完整回答
反對 回復 2023-07-19
  • 3 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號