从创建数组列表的Android JSON数组 [英] creating json array from arraylist android
问题描述
我一直在这个周围的玩弄了一会儿,我寻找一些帮助。我有一个的ArrayList
,我变成一个 JSONArray
,然后把里面的的JSONObject
。问题是我的格式。让我告诉你。
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.
我得到的价值为我的的ArrayList
并添加他们像这样(小code段)
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
现在,当我创建我的 JSON
来送过来我用下面的
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();
}
再次工作的。现在清楚地说明这个问题,它打印出 JSON
像这样
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"
]
}
如果你熟悉 JSON
你知道我在这里疯玩了。我只是试图保持问答在一起。但是通过添加:中间它看起来像我试图使用问题为键
并回答为值
(种)。不管怎么说到底这看起来像合法的 JSON
,它是,但它不工作,我需要的方式它。
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
?
如果让我将如何去创造我的 JSONArray
让我的 JSON
看起来像这样
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",
....
]
}
这样一个简单的
var_dump(json_decode($json));
var_dump(json_decode($json, true));
在 PHP
将提供了我需要服务器端的数据。
in PHP
will provide me with the data I need server side.
或交替就只是简单的把这个格式,并分割字符串作为我解析这些服务器端?无论回答您选择,请提供code。与来说明你的回答几行。正如你可以告诉我是一个视觉化的人。
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!
推荐答案
您可以解决这个一大堆方法。如果您想保留原来的格式事情是这样的,你只需要先处理你的字符串。当您从的ArrayList
刚刚拆分组合成几部分获取值
You can solve this a bunch of ways. If you want to keep the formatting the way it is you just need to process your strings first. When you get values from your ArrayList
just split the combo into parts
int splitPoint = combo.indexOf(":")
String key = combo.substring(0, splitPoint);
String value = combo.substring(splitPoint + 1);
一旦分割键和值出刚刚创建一个新的的JSONObject
并添加键和值作为字符串
Once you split the key and value out just create a new JSONObject
and add the key and value as a string
JSONObject jObject = new JSONObject();
jObject.put(key, value);
这将JSONString加入到自己的JSONObject。然后,你可以要创建这个对象添加到JSONArray
This will add the JSONString to its own JSONObject. Then you can add this object to the JSONArray that you want to create
JSONArray jArray = new JSONArray();
jArray.puJSONObject(jObject);
我在零件打破了这一下来,但如果你只是decalre你的 JSONArray
ouside一个for循环或其他一些迭代器和循环通过的ArrayList
,处理每个组合串并添加生成的对象为 JSONArray
可以acheive所期望的结果。
I broke this down in to parts but if you just decalre your JSONArray
ouside a for loop or some other iterator and loop through your ArrayList
, processing each combo string and adding the resulting object to a JSONArray
you can acheive the desired result
这篇关于从创建数组列表的Android JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!