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
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 :)
The json structure should look like as below [ { "Key1" : "value1", "Key2" : "value2", }, { "Key1" : "value1", "Key2" : "value2", }
]
The json structure should be like this :
[{"quantity" : 2, "price" : 15, "food" : "Fried Rice"}, {"quantity" : 2, "price" : 20, "food" : "Rice and Stew"}]
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
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
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
I have a Cordova application that contains the Company Contacts
I am trying to make blocks of data (10 each) as query