如何更新特定的JSON值?

时间:2014-06-21 07:31:59

标签: android json android-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",
        "BattingDetail": [
            {
                "sixes": "0",
                "balls": "0",
                "runs": "0", //want update this variable values from 0 to 10
                "player": "Batsman 1",
                "fours": "0",
                "battingposition": "1"
            },
            {
                "sixes": "0",
                "balls": "0",
                "runs": "0",
                "player": "Batsman 2",
                "fours": "0",
                "battingposition": "1"
            }
        ],
        "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"
}
}

从上面的JSON结构我想在 BattingDetail 数组的第一个索引中更新运行变量。无法解决此问题。任何人都知道帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

如果您正在使用http://developer.android.com/reference/org/json/JSONObject.html

中的JSONObject

BattlingDetail JSONArray获取相应的JSONObject。或者根据您的需要迭代阵列。删除当前值。换一个。

JSONObject object = array.getJSONObject[0];
object.remove("runs");
object.put("runs", 100);