3
\$\begingroup\$

I wanted to load in map information using JSON, is there a way to get String, Int, float etc lists or arrays from JSON in Libgdx?

My structure looks like this

{
    "data": [{
        "name": "startmap",
        "actors": [{
            "name": "ryder"
        }]
    }]
}

And I want to get Actors as either a String List or Array, but there is no GetList or GetArray function. Is there any other way to do this?

\$\endgroup\$
1
\$\begingroup\$

I have found the solution. First I add a JsonValue to my map object. Then I add a String list to my map object as well. JsonValue has this handy method called Child, wich allows me to loop through every child in the JsonValue, wich I add to my list of Strings.

The code looks like this.

newMap.actorstorage = saveJson.get("actors");
    for (JsonValue actor = newMap.actorstorage.child; actor != null; actor = actor.next){
        for(int c = 0; c < actor.size; c++){
            newMap.actors.add(actor.getString(c));
    }
}

Wich returns something like this

[name1,name2]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.