Android的加入JSON对象为Array [英] Android adding Json objects into an Array
问题描述
我想要做的就是创建一个包含由一个String组织的其他一个JSONObjects数组一个JSONObject?例如我想创建一个包含包含灯具对象的数组也由比赛日期组织一个JSONObject
给你什么,我想acheive视觉参考
的JSONObject(JSONArray(对象12年10月10日{(夹具内容)(夹具内容)})
(对象12年11月10日{(夹具内容)(夹具内容)}))
继承人是我迄今为止尝试过,但就是无法得到它的工作。
字符串matchDate1 = NULL;
JSONArray datesArray = NULL;
JSONObject的fixturesInfo = NULL;
JSONArray fixturesInfoArray = NULL;
字符串matchDateTemp = NULL; 为(中间体F = 0; F&下; fixturesArray.length();˚F++){ JSONObject的matchDateDict = fixturesArray.getJSONObject(F);
matchDate1 = matchDateDict.getString(matchdate);
JSONArray fixturesInfoDict = fixturesInfo.getJSONArray(matchDate1); 如果(fixturesInfoDict == NULL){
tempArray = NULL;
}其他{
tempArray = fixturesInfoDict;
} 如果(matchDateTemp!= matchDate1){
fixturesInfoArray.put(matchDate1);
} matchDateTemp = matchDate1; tempArray.put(fixturesArray.getJSONObject(F));
fixturesInfo.put(matchDate1,tempArray); }
Log.v(MyFix,fixturesInfo =+ fixturesInfo); }赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
}
继承人的JSON提要
{
code:200,
错误:空,
数据:{
灯具:[{
开球:15:00:00,
matchdate:2012-07-14,
homescore:空,
awayscore:空,
考勤:空,
homepens:空,
awaypens:空,
division_id:5059,
师:测试1
补偿:LGE
位置:空,
fixture_note:空,
hometeam_id:64930,
hometeam:团队1
awayteam_id:64933,
awayteam:团队4
},{
开球:15:00:00,
matchdate:2012-07-14,
homescore:空,
awayscore:空,
考勤:空,
homepens:空,
awaypens:空,
division_id:5059,
师:测试1
补偿:LGE
位置:空,
fixture_note:空,
hometeam_id:64935,
hometeam:团队6,
awayteam_id:64937,
awayteam:八队
},{
开球:15:00:00,
matchdate:2012-07-28,
homescore:空,
awayscore:空,
考勤:空,
homepens:空,
awaypens:空,
division_id:5059,
师:测试1
补偿:LGE
位置:空,
fixture_note:空,
hometeam_id:64930,
hometeam:团队1
awayteam_id:64931,
awayteam:二队
},{
开球:15:00:00,
matchdate:2012-07-28,
homescore:空,
awayscore:空,
考勤:空,
homepens:空,
awaypens:空,
division_id:5059,
师:测试1
补偿:LGE
位置:空,
fixture_note:空,
hometeam_id:64930,
hometeam:团队1
awayteam_id:64931,
awayteam:二队
}]
}
}
从你说什么,你似乎是想建立一个JSONArray一些JSON对象。这可能帮助:
公共无效writeJSON(){
JSONObject的用户=新的JSONObject();
用户2的JSONObject;
用户2 =新的JSONObject();
尝试{
user.put(dish_id,1);
user.put(dish_custom,2);
user.put(量,2);
user.put(共享,2); user2.put(dish_id,2);
user2.put(dish_custom,2);
user2.put(量,4);
user2.put(共享,3);
}赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
} JSONArray notebookUsers =新JSONArray();
notebookUsers.put(用户);
notebookUsers.put(用户2);
的System.out.println(JSON数组是+ notebookUsers);
下面2 JSON对象添加到1 JSONArray。
在的System.out.println的输出会是这个样子:
JSON数组is[{\"shared\":\"2\",\"dish_custom\":\"2\",\"dish_id\":\"1\",\"quantity\":\"2\"},{\"shared\":\"3\",\"dish_custom\":\"2\",\"dish_id\":\"2\",\"quantity\":\"4\"}]
和您正在使用的字符串比较是不正确的。
如果(matchDateTemp!= matchDate1)
你无法比较喜欢的字符串。 ü可以使用这样的:
如果(!(matchDateTemp.equals(matchDate1)))
What I'm trying to do is create a JSONObject that contains an Array of other JSONObjects that are organized by a String? for example i want to create a JSONObject that contains an array of objects that contain fixtures that are also organized by match date
to give you a visual reference of what i am trying to acheive
JSONObject (JSONArray("Object 10/10/12" {(fixtures content)(fixtures content)}")
("Object 11/10/12" {(fixtures content)(fixtures content)}"))
heres what i have tried so far but just can't get it to work
String matchDate1 = null;
JSONArray datesArray = null;
JSONObject fixturesInfo = null;
JSONArray fixturesInfoArray = null;
String matchDateTemp = null;
for(int f = 0; f < fixturesArray.length(); f++){
JSONObject matchDateDict = fixturesArray.getJSONObject(f);
matchDate1 = matchDateDict.getString("matchdate");
JSONArray fixturesInfoDict = fixturesInfo.getJSONArray(matchDate1);
if(fixturesInfoDict == null){
tempArray = null;
} else {
tempArray = fixturesInfoDict;
}
if(matchDateTemp != matchDate1){
fixturesInfoArray.put(matchDate1);
}
matchDateTemp = matchDate1;
tempArray.put(fixturesArray.getJSONObject(f));
fixturesInfo.put(matchDate1, tempArray);
}
Log.v("MyFix", "fixturesInfo = " + fixturesInfo);
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
heres the json feed
{
"code": 200,
"error": null,
"data": {
"fixtures": [{
"kickoff": "15:00:00",
"matchdate": "2012-07-14",
"homescore": null,
"awayscore": null,
"attendance": null,
"homepens": null,
"awaypens": null,
"division_id": "5059",
"division": "Testing 1",
"comp": "LGE",
"location": null,
"fixture_note": null,
"hometeam_id": "64930",
"hometeam": "Team 1",
"awayteam_id": "64933",
"awayteam": "Team 4"
}, {
"kickoff": "15:00:00",
"matchdate": "2012-07-14",
"homescore": null,
"awayscore": null,
"attendance": null,
"homepens": null,
"awaypens": null,
"division_id": "5059",
"division": "Testing 1",
"comp": "LGE",
"location": null,
"fixture_note": null,
"hometeam_id": "64935",
"hometeam": "Team 6",
"awayteam_id": "64937",
"awayteam": "Team 8"
}, {
"kickoff": "15:00:00",
"matchdate": "2012-07-28",
"homescore": null,
"awayscore": null,
"attendance": null,
"homepens": null,
"awaypens": null,
"division_id": "5059",
"division": "Testing 1",
"comp": "LGE",
"location": null,
"fixture_note": null,
"hometeam_id": "64930",
"hometeam": "Team 1",
"awayteam_id": "64931",
"awayteam": "Team 2"
}, {
"kickoff": "15:00:00",
"matchdate": "2012-07-28",
"homescore": null,
"awayscore": null,
"attendance": null,
"homepens": null,
"awaypens": null,
"division_id": "5059",
"division": "Testing 1",
"comp": "LGE",
"location": null,
"fixture_note": null,
"hometeam_id": "64930",
"hometeam": "Team 1",
"awayteam_id": "64931",
"awayteam": "Team 2"
}]
}
}
From what you say, you seem like trying to build a JSONArray out of some JSON Objects. This might help:
public void writeJSON() {
JSONObject user = new JSONObject();
JSONObject user2;
user2 = new JSONObject();
try {
user.put("dish_id", "1");
user.put("dish_custom", "2");
user.put("quantity", "2");
user.put("shared", "2");
user2.put("dish_id", "2");
user2.put("dish_custom", "2");
user2.put("quantity", "4");
user2.put("shared", "3");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray notebookUsers = new JSONArray();
notebookUsers.put(user);
notebookUsers.put(user2);
System.out.println("the JSON ARRAY is"+notebookUsers);
Here 2 json objects are added to 1 JSONArray.
The output of the System.Out.println will look something like this:
the JSON ARRAY is[{"shared":"2","dish_custom":"2","dish_id":"1","quantity":"2"},{"shared":"3","dish_custom":"2","dish_id":"2","quantity":"4"}]
And the string comparison you are using is not right.
if(matchDateTemp != matchDate1)
u cannot compare strings like that. u can use something like:
if(!(matchDateTemp.equals(matchDate1)))
这篇关于Android的加入JSON对象为Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!