Cannot make json valid for recyclerview

134
September 10, 2019, at 1:40 PM

I'm trying to JSON using Gson and recyclerview. My JSON isnt completely valid. In my JSON, the food field has only one of the string quoted leaving the other unquoted. Kindly see my JSON below...

[
    {"quantity" = 2, 
    "price" = 15, 
    "food" = "Fried" Rice}, 
    {"quantity" = 2,
    "price" = 20,
    "food" = "Rice" and Stew}
    ]

You can see that Fried is in quotes and Rice isnt in quotes, likewise the same for Rice and Stew in the other too. Initially it was like this ...

[
    {quantity = 2, 
    price = 15, 
    food = Fried Rice}, 
    {quantity = 2,
    price = 20,
    food = Rice and Stew}
    ]

My activity class code...

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);
    }

I need the food field that has Fried Rice to be inbetween quotes as one and the same for rice and stew or if theres a workaround, I would like to know.

Thank you

Answer 1

Base on your code. (Try to convert listOfFood to JSON ) I modify 2 lines of your code as bellow

        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 :) 
Answer 2

The json structure should look like as below [ { "Key1" : "value1", "Key2" : "value2", }, { "Key1" : "value1", "Key2" : "value2", }

]

Answer 3

The json structure should be like this :

[{"quantity" : 2, "price" : 15, "food" : "Fried Rice"}, {"quantity" : 2, "price" : 20, "food" : "Rice and Stew"}]

Rent Charter Buses Company
READ ALSO
Xamarin WebView load URL after special page

Xamarin WebView load URL after special page

Hello I try to authenticate some siteAfter login page that site redirect my webview to another site and new url has important parameter for me

155
Looking for Java XML parser similar to Expat parser in C++ [on hold]

Looking for Java XML parser similar to Expat parser in C++ [on hold]

I am trying to write a common XML parser class in java similar to the one written in C++In c++, they build the solution using Expat parser library

151
There is a Cordova plugin to identify caller on Android and Ios Platforms?

There is a Cordova plugin to identify caller on Android and Ios Platforms?

I have a Cordova application that contains the Company Contacts

140
Is there a way to use both limitToLast and limitToFirst at the same time?

Is there a way to use both limitToLast and limitToFirst at the same time?

I am trying to make blocks of data (10 each) as query

129