-1

I'm working on a D3 DPS calculator and i'm having a problem with a JSON-Object. I get a JSON-Object like this:

{"Sockets":{"min":1,"max":1},
"Dexterity_Item":{"min":165,"max":165},
"Durability_Cur":{"min":581,"max":581},
"Durability_Max":{"min":715,"max":715},
"Attacks_Per_Second_Item_Percent":{"min":0.1,"max":0.1},
"Damage_Weapon_Delta#Arcane":{"min":315,"max":315},
"Damage_Weapon_Min#Arcane":{"min":274,"max":274},
"Damage_Weapon_Delta#Physical":{"min":161,"max":161},
"Damage_Weapon_Min#Physical":{"min":190,"max":190},
"Attacks_Per_Second_Item":{"min":1.2000000476837158,"max":1.2000000476837158},
"Steal_Health_Percent":{"min":0.03,"max":0.03}}

How can i split all of this values? I cant do it by name, because its "random". I would like to have a List with the stat and the value.

user2375222
  • 1
  • 1
  • 1
  • 1

4 Answers4

0

One thing that might help is to view your JSON in an online JSON viewer like this. You can hit format, and get something out of it that looks like this:

{
  "Sockets": {
    "min": 1,
    "max": 1
  },
  "Dexterity_Item": {
    "min": 165,
    "max": 165
  },
  "Durability_Cur": {
    "min": 581,
    "max": 581
  },
  "Durability_Max": {
    "min": 715,
    "max": 715
  },
  "Attacks_Per_Second_Item_Percent": {
    "min": 0.1,
    "max": 0.1
  },
  "Damage_Weapon_Delta#Arcane": {
    "min": 315,
    "max": 315
  },
  "Damage_Weapon_Min#Arcane": {
    "min": 274,
    "max": 274
  },
  "Damage_Weapon_Delta#Physical": {
    "min": 161,
    "max": 161
  },
  "Damage_Weapon_Min#Physical": {
    "min": 190,
    "max": 190
  },
  "Attacks_Per_Second_Item": {
    "min": 1.2000000476837158,
    "max": 1.2000000476837158
  },
  "Steal_Health_Percent": {
    "min": 0.03,
    "max": 0.03
  }
}

Also I really like this description for Android JSON parsing

Community
  • 1
  • 1
HalR
  • 11,411
  • 5
  • 48
  • 80
0

If you develop on android platform, you can easily use the json api: http://developer.android.com/reference/org/json/JSONObject.html

You have just to instanciate a JSONObject:

JSONObject jsonObject = new JSONObject(myStringData)

and get your data with each key from your json String (min, max,...)

like this:

int min = jsonObject.getInt("min");

I hope this will help.

Theo
  • 57,719
  • 8
  • 24
  • 41
Jarvis
  • 1,527
  • 12
  • 17
0

Just an exemple for using JSON api

JSON File:

{
    "chapitres":{

        "chapitre":[
            {   
                "id": "1",
                "name": "La plateforme Android 1",
                "desc": "Description chapitre 1"
            },

            {   
                "id": "2",
                "name": "La plateforme Android 2",
                "desc": "Description chapitre 2"
            },

            {   
                "id": "3",
                "name": "La plateforme Android 3",
                "desc": "Description chapitre 3"
            },
            {   
                "id": "4",
                "name": "La plateforme Android 4",
                "desc": "Description chapitre 4"
            }
        ]
    }
}

JAVA CODE:

JSONObject jsonObject = new JSONObject(json);

JSONObject chapitresJsonObject = jsonObject.getJSONObject("chapitres");

JSONArray chapitreJsonArray = chapitresJsonObject.getJSONArray("chapitre");

    for (int i = 0; i < chapitreJsonArray.length(); i++) {

        JSONObject ChapJsonObject = chapitreJsonArray.getJSONObject(i);

        String id = ChapJsonObject.getString("id");
        String name = ChapJsonObject.getString("name");
        String desc = ChapJsonObject.getString("desc");
         }

that's all

Jarvis
  • 1,527
  • 12
  • 17
0

If you don't know the names of your stats, you can get them with JSONObject.names().

JSONObject json = new JSONObject(...);

JSONArray stats = json.names();
for (int i = 0; i < stats.length(); i++) {
    String name = stats.getString(i);
    JSONObject stat = json.getJSONObject(name);

    stat.getInt("min");
    stat.getInt("max");
}
brillenheini
  • 5,413
  • 3
  • 23
  • 20