-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, }]

7
  • can you place some sample data so that we can check it Commented May 5, 2018 at 17:37
  • @Alok what do you mean? Text of this String? Commented May 5, 2018 at 17:40
  • yes some part of string so that we can check it, if it is valid data or not. Commented May 5, 2018 at 17:41
  • 1
    Please give a small (truncated) example of the string you say is a "... typical JSONArray". Commented May 5, 2018 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. Commented May 5, 2018 at 17:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

12 Comments

i got again "unhandled exception org.json.jsonexception". on this code "JSONArray(toConvert)"
it means your String is probably a not valid json array as you said
Ohhh yea, sorry, i made mistake.. look at my question now. I want to conver to ArrayList<Object>
No problem, some people don't understand that if they downvote someone it is polite to explain why :/
I agree @LeviAlbuquerque. I just don't understand people down voting valid answers without at least saying why it was down voted. Maybe that person will eventually get up some courage to explain themselves! An Up vote to compensate.
|
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.

Comments

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));

2 Comments

How to use this? I must write something build.gradle? what?
you must add dependency of jackson api in your build.gralde ( just like another dependency and use the above code )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.