简体   繁体   中英

Comma is causing unwanted string split

I have an array of data sent from my database - Once received, I save it in shared preferences - here is my getter:

public List getAnswerStringEdit() {
        return answer_edit;
    }

I save it as so:

editor.putString(Constants.ANSWER_EDIT,resp.getAnswer().getAnswerStringEdit().toString().trim());

Then retrieve it here:

String answerString = pref.getString(Constants.ANSWER_EDIT, "").trim();
        answerString = answerString.substring(1, answerString.length() - 1).trim();
        String[] array = answerString.split(",");

Finally, I access the array as so:

et_answer1_edit.append(array[0]);

My problem is this - Say I add a questions which has a comma in the middle of it, like -

Question 1- " Why is this broke, I don't know? "

Currently, when I retrieve my question, the string is getting split, even though there are quotation marks around the whole question/answer- So in the example above, in position 0 in the array, I should have:

" Why is this broke, I don't know ?"

However, instead I am getting in position 0:

Why is this broke - then position 1 as: I don't know

I know this sounds daft because clearly, I am calling for the split to happen on the comma, but I expect that at the end of the whole string object, not in the middle of it.

The retrieved JSON is as follows:

{
    "result": "success",
    "message": "Answer Has Been Selected",
    "answer": {
        "answer_edit": ["Why is this broke, I don't know?", "What is your favorite song, because I want to know"]
    }
}

Any help/advice that can help me to understand what is causing this, would be really appreciated.

Dont split the string using ',' use this

JSONObject jsonObject = new JSONObject(answerString );
            JSONArray jsonArray = jsonObject.getJSONObject("answer").getJSONArray("answer_edit");
            Log.e("Json Array elements are","First Element : "+jsonArray.get(0)+"\nSecond Element : "+jsonArray.get(1));
            String QuestionString1 = jsonArray.get(0).toString();
            String QuestionString2 = jsonArray.get(1).toString();

Try this

JSONObject jsonObject = new JSONObject("your json response");

try 
  {
      JSONObject data = jsonObject.getJSONObject("answer");

      JSONArray jsonArray = data.getJSONArray("answer_edit");
      Log.e("=>", "" + jsonArray);

      for (int i = 0; i < jsonArray.length(); i++) 
      {
         String value = jsonArray.getString(i);

         String[] parts = value.split(Pattern.quote(","));

          for (int j=0; j<parts.length; j++)
          {
            Log.e("Answer String ", "=" + parts[j]);
          }
     }
  } catch (JSONException e) {
      e.printStackTrace();
  }

OUTPUT

E/=>: ["Why is this broke, I don't know?","What is your favorite song, because I want to know"]
E/Answer String: =Why is this broke
E/Answer String: = I don't know?
E/Answer String: =What is your favorite song
E/Answer String: = because I want to know

try this one

JSONObject jsonObject = new JSONObject("your json response");

try 
  {
      JSONObject answer= jsonObject.getJSONObject("answer");

      JSONArray jsonArrayAnswerEdit = answer.getJSONArray("answer_edit");
      Log.e("=>", "" + jsonArrayAnswerEdit);
      for (int i = 0; i < jsonArrayAnswerEdit.length(); i++){
          String que= jsonArrayAnswerEdit.getString(i);
          Log.e("json", i + "=" + que);
     }
  } catch (JSONException e) {
      e.printStackTrace();
  }

After reading all the suggest answers, figured out a simple solution:

First I stored my answers sent from my external database as so -

final String jsonAnswers = gson.toJson (resp.getAnswer().getAnswerStringEdit());

Then saved in shared pref -

editor.putString(Constants.ANSWER_EDIT,jsonAnswers);

Next to read the answer back out:

String answerString = pref.getString(Constants.ANSWER_EDIT, "").trim();
final String[] array = gson.fromJson (answerString, String[].class);

Finally, I could set my Edittext with data from the array:

et_answer1_edit.append(array[0].trim());

et_answer2_edit.append(array[1].trim());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM