I've been toying around with this for a little while and I'm looking for some help. I have an ArrayList that I turn into a JSONArray and then place inside JSONObject. The problem is my formatting. Allow me to show you.
I get the values for my ArrayList and add them like so (small code snippet)
if (v instanceof EditText) {
String answer = ((EditText) v).getText().toString();
if (answer.equals("")) {
error.setText(v.getTag().toString()
+ " Needs an answer");
((EditText) v).setHint("Fill me out!");
((EditText) v).setHintTextColor(getResources()
.getColor(R.color.red_bg));
} else {
error.setText("");
}
String question = v.getTag().toString();
String combo = question + ": " + answer;
myList.add(combo);
Log.v("INFO", "Storing: " + combo);
}
This works, but adding the ":" is the start of my problems. The log prints out
06-19 12:13:33.630: V/INFO(3272): Storing: Height: 6`4
Now when I create my JSON to be sent over I use the following
if (error.getText().toString().equals("")) {
JSONObject json = new JSONObject();
JSONArray jArray = new JSONArray(myList);
try {
json.putOpt("array", jArray);
} catch (JSONException e) {
e.printStackTrace();
}
Again, this works. Now the problem clearly illustrated, it prints out JSON like this
{
"array":
[
"Store #: 00608",
"Phone #: null",
"Address: 3014 N. SCOTTSDALE RD.",
"City: SCOTTSDALE",
"Zip: 85251",
"State: AZ",
"Height: 6`4",
"Weight: 230",
"Ethnicity: White",
"Age: 23",
"Eye Color: Blue",
"Favorite Food: Thai",
"Comments: awesome"
]
}
If you're familiar with JSON you know I've goofed it here. I was merely trying to keep the question and answer together. However by adding the ":" to the middle it looks like I'm trying to use question as the key and answer as the value (kind of). Anyways in the end this looks like legitimate JSON, and it is, but it doesn't work the way I need it to.
My question is, should I just make "question" the key, and "answer" the value?
If so how would I go about creating my JSONArray so that my JSON looks like this
{
"array":
[
"Store #" : "00608",
"Phone #" : "null",
"Address" : "3014 N. SCOTTSDALE RD.",
"City" : "SCOTTSDALE",
"Zip" : "85251",
"State" : "AZ",
"Height" : "6`4",
"Weight" : "230",
....
]
}
So that a simple
var_dump(json_decode($json));
var_dump(json_decode($json, true));
in PHP will provide me with the data I need server side.
or alternatively would it just be simpler to keep this format and split the strings as I parse them server side? Whichever answer you choose please supply a few lines of code with to illustrate your answer. As you can tell I'm a visual person.
Thanks for the help, I hope this helps other people in the future as well!