-1

I have String "[{...}]" and I want convert it to JSONArray, and then to List list = new List I was trying this solution, and this is my code

public void RestoreData()
{
    String toConvert = "[{...}]" // I don't place full String, but it's typical JSONArray, but in String
    ArrayList<myClass> listdata = new ArrayList<myClass>();
    JsonObject jsonObject = new JsonObject();
    JSONArray jArray = (JSONArray)jsonObject;
    if (jArray != null) {
        for (int i=0;i<jArray.length();i++){
            listdata.add(jArray.getString(i));
        }
    }
}

And when I'm trying to compile this I get 2 errors:

  • In JSONArray jArray = (JSONArray)jsonObject; I get error 'Incovertible types; cannot cast 'org.json.JSONObject' to 'org.json.JSONArray'.
  • And second: in listdata.add(jArray.getString(i)); I get error 'unhandled exception org.json.JSONException'.

I'm new in Java and I work with Json for the first time.

EDIT A small truncated example of the Json string:

[{"lootArmorGain":0,"lootBlockGain":0,"lootCost":93500,"lootCritGain":2,"lootCritPowerGain":0,"lootDamageAbsorptionGain":0,"lootDamageGain":0, }]

AmiNadimi
  • 5,129
  • 3
  • 39
  • 55
Hubizonek
  • 19
  • 4
  • can you place some sample data so that we can check it – Alok May 05 '18 at 17:37
  • @Alok what do you mean? Text of this String? – Hubizonek May 05 '18 at 17:40
  • yes some part of string so that we can check it, if it is valid data or not. – Alok May 05 '18 at 17:41
  • 1
    Please give a small (truncated) example of the string you say is a "... typical JSONArray". – Barns May 05 '18 at 17:48
  • Now we need to know what `myClass` is. By-the-way in java naming convention methods do not use capitalization... however, Classes are capitalized. – Barns May 05 '18 at 17:51
  • I made a mistake. It's Object Array, and this is fragment of this String:[{"lootArmorGain":0,"lootBlockGain":0,"lootCost":93500,"lootCritGain":2,"lootCritPowerGain":0,"lootDamageAbsorptionGain":0,"lootDamageGain":0, }] – Hubizonek May 05 '18 at 17:51
  • myClass is a class created by me: public class myClass() { public myClass(....) { /*code */ } /* code */ } – Hubizonek May 05 '18 at 17:53

3 Answers3

3

I think what you want is:

public void RestoreData()
{
   try{
    String toConvert = "[{...}]" // I don't place full String, but it's typical JSONArray, but in String
    ArrayList<MyClass> listdata = new ArrayList<MyClass>();
    JSONArray jsonArray = new JSONArray(toConvert); 

    if (jsonArray != null) {
       Gson gson = new Gson();
        for (int i=0;i<jsonArray.length();i++){
            String json = jsonArray.getJSONObject(i).toString();
            MyClass obj = gson.fromJson(json, MyClass.class);
            listdata.add(obj);
        }
    }
    }catch(JSONException e){
          e.printStackTrace();
    }
}

To convert from JSONOject to your custom class use GSON, see above.

 compile 'com.google.code.gson:gson:2.8.4'

Note that your custom class need to have an empty constructor as well as getters and setters in order to make gson work.

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
1

I think best approach will be using Google Gson Library.

String toConvert = "[{...}]"
Type listType = new TypeToken<List<myClass>>() {}.getType();
List<myClass> yourList = new Gson().fromJson(toConvert, listType);

You dont need to get each position manually.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

For simplicity you can also use Jackson api

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<SomeClass> someClassList = objectMapper.readValue(jsonString,typeFactory.constructCollectionType(List.class, SomeClass.class));
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
Naveen
  • 830
  • 9
  • 19