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.

4
  • Why dont you just save the original string? (The first one in your post). Commented Nov 27, 2017 at 10:37
  • Check if is this what you need? Commented Nov 27, 2017 at 10:38
  • And why is removing such backslashes a pine? Commented Nov 27, 2017 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. Commented Nov 27, 2017 at 10:42

2 Answers 2

0

Try converting to a List before converting to Json

Arrays.asList(urlsArray)
Sign up to request clarification or add additional context in comments.

Comments

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("\\","");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.