如何在现有JSON数组中插入JSON对象?

时间:2014-06-21 09:28:57

标签: android json android-json

这里以静态方式创建了JSON值。

JSON结构

{
"MatchType": {
    "inningsperteam": "1",
    "maxovers": "5",
    "players": "11"
},
"Innings": [
    {
        "noballs": "0",
        "BowlingDetail": [
            {
                "noballs": "0",
                "balls": "0",
                "maidens": "0",
                "player": "Bowler 12",
                "runs": "0",
                "wickets": "0",
                "bowlingposition": "1",
                "wides": "0"
            }
        ],
        "byes": "0",
        "battingteam": "Team 26",
        "overs": "0",
        "addpenaltyruns": "0",
        "legbyes": "0",
        "inplay": "true",
        "balls": "0",
        "InningsOvers": [],
        "declared": "false",
        "penaltyruns": "0",
        "bowlingteam": "Team 46",
        "runs": "0",
        "wickets": "0",
        "wides": "0",
        "completed": "false"
    }
],
"Umpire 1": "Umpire 12",
"Umpire 2": "Umpire 53",
"location": "England",
"Ground": "Oval",
"date": "20/6/2014",
"Club": [
    {
        "Team": {
            "Player": [
                {
                    "keeper": "false",
                    "captain": "false",
                    "firstname": "Batsman 1"
                },
                {
                    "keeper": "false",
                    "captain": "false",
                    "firstname": "Batsman 2"
                }
            ],
            "local": "1",
            "name": "Team 26",
            "toss": "false"
        },
        "local": "1",
        "name": "Club1"
    },
    {
        "Team": {
            "Player": [
                {
                    "keeper": "false",
                    "captain": "true",
                    "firstname": "Bowler 12"
                }
            ],
            "local": "2",
            "name": "Team 46",
            "toss": "true"
        },
        "local": "2",
        "name": "Club2"
    }
],
"CurrentPlayers": {
    "bowler": "Bowler 12",
    "playerA": "Batsman 1",
    "playerB": "Batsman 2"
}
}

BattingDetails JSON

[
{
    "BattingDetail": [
        {
            "balls": "0",
            "sixes": "0",
            "runs": "10",
            "player": "Batsman 1",
            "battingposition": "1",
            "fours": "0"
        },
        {
            "balls": "0",
            "sixes": "0",
            "runs": "0",
            "player": "Batsman 2",
            "battingposition": "1",
            "fours": "0"
        }
    ]
}
]

编码

try {

        json_scores = new JSONObject(test_str);

        jsonArray = new JSONArray("["+test_str+"]");
        json_scores = jsonArray.getJSONObject(0);

        JSONArray nxt_test = new JSONArray(json_scores.getString("Innings"));

        String str = new JSONArray(json_scores.getString("Innings")).getJSONObject(0).getString("BattingDetail");
        JSONArray jArray = new JSONArray(str);
        for (int i = 0; i < jArray.length(); i++) {

            JSONObject json = new JSONObject();
            json = jArray.getJSONObject(i);

            if (json.getString("player").equalsIgnoreCase(player_name)) {

                jArray.getJSONObject(i).put("runs", "10");

            }
        }


        Log.v("Test Array", ""+nxt_test.put(0,new JSONObject().put("BattingDetail", jArray)));

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

我想将 BattingDetail JSON数组放在JSON Structure.am中的局内 json数组中,但是无法解决这个问题。任何人都知道帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

将主json数据转换为JSONObject。

JSONObject jsobj=new JSONObject(data);

JSONArray jsarBatting=new JSONArray(battingdetail);

JSONArray jsAr=jsobj.getJSONArray("innings"); 

JSONObject jsobj=  jsAr.getJSONObject(0); 

jsobj.put("BattingDetail",jsarBatting);