0

I want to convert this array of youtube URLs to a JSON Array to be saved in an android SQL database.

[https://www.youtube.com/watch?v=FnCdOQsX5kc, https://www.youtube.com/watch?v=xKJmEC5ieOk]

It might be extended with other attributes at a later point so I encode with this:

 JSONObject mJsonObject = new JSONObject();
 mJsonObject.put(MoviesDBJsonUtils.MDB_JSON_OBJ_KEY, new JSONArray(detailsCellEntry));
 String urlArrayList = mJsonObject.toString();

It should now all be ready for saving into a SQL text field. But it gives me:

{"DB_JSON_OBJ_KEY":["https:\/\/www.youtube.com\/watch?v=FnCdOQsX5kc","https:\/\/www.youtube.com\/watch?v=xKJmEC5ieOk"]}

It's backslashing all the forward slashes. This is a pain in the butt. What's the best way of preventing this or remedying this?

I'm tempted to just scrap it and import another external library Gson, but bloating it up with libraries feels lazy.

Avinash
  • 361
  • 4
  • 16
Sam
  • 1,659
  • 4
  • 23
  • 42
  • Why dont you just save the original string? (The first one in your post). – greenapps Nov 27 '17 at 10:37
  • [Check if is this what you need?](https://stackoverflow.com/a/5166890/3619945) – Tiago_nes Nov 27 '17 at 10:38
  • And why is removing such backslashes a pine? – greenapps Nov 27 '17 at 10:38
  • greenapps, that's what I originally did but the insertion put it in double [[array 1, array 2 ]] this pushed me over. I've decided I may want to store more things in the object, other than a url array, so I'm setting it up. Greenapps - it's an unnecessary additional loop over the whole array to strip out the backslashes. – Sam Nov 27 '17 at 10:42

2 Answers2

0

Try converting to a List before converting to Json

Arrays.asList(urlsArray)
Tiago_nes
  • 843
  • 7
  • 30
0

First convert your json object to string

String urlArrayList = mJsonObject.toString();

than replace all \ with below function than use myurl in your purpose

String myUrl = urlArrayList .replace("\\","");
Babul
  • 1,076
  • 7
  • 9