How I can parse json with multiple data, example I have json:
[
{
name: "test1.png",
x: 41000,
y: 40000
},
{
name: "test2.png",
x: 42000,
y: 40000
}
]
How I can parse json with multiple data, example I have json:
[
{
name: "test1.png",
x: 41000,
y: 40000
},
{
name: "test2.png",
x: 42000,
y: 40000
}
]
Well, what are you trying to parse those values to?
Do they have a class of their own? Are they fields within the same class? Are they array’s of objects that contain such fields?
Yes their have class
public class MapJsonReader {
public String name;
public float x;
public float y;
}
you propose to add a class with an array of this class? e.g.
public class MapArrayJsonReader {
public MapJsonReader array
}
and parse:
Json json = new Json();
MapJsonReader map = json.fromJson(MapArrayJsonReader.class, Gdx.files.internal("assets/cfg/map.json"));
I thought the same just wanted to make in this approach
You need to create node structure or something like that, or you could use json parser, which you can google up, though I haven’t used any so can’t say which are good ones.
If you want to parse it, you can try something like this.
Read characters from string one by one. When you find certain character, do a certain thing. For example, if you encounter ‘{’ you know that you need to create a new node.
Here is how your node system would look like.
master node
node
name
x
y
node
name
x
y
You might wanna try using my parser101 if you’re not using json to transfer data from some kind of other application, like 3d modeling software of something like that.
Generally you’d use a library to do the parsing for you to make life easier. You COULD do it on your own but please don’t.
http://www.json.org/
http://www.json.org/java/index.html
With these any of these libs you can parse json text into familiar java objects that you can work with.
Hm,
I usually just parse and entire object into a Json file, like an entire level, which contains 100’s of other objects.
From what I can see, you are trying to parse in 2 of the same object, into nothing. If that makes sense?
Have a look at how that is structured, a json file is basically an object written to disc, afaik unless you have your own parse algorithm setup, you can only parse 1 object from that single file, however that object can contain many other objects.
All thanks, I understood. I create json
{
"array": [
{
"name": "17",
"x": 40000,
"y": 40000
},
{
"name": "2",
"x": 41000,
"y": 40000
},
{
"name": "11",
"x": 42000,
"y": 40000
},
{
"name": "32",
"x": 43000,
"y": 40000
}
]
}
Two java class
public class MapJsonReader {
public String name;
public float x;
public float y;
}
public class MapArrayJsonReader {
public ArrayList array;
}
And this code in project
Json json = new Json();
json.setElementType(MapArrayJsonReader.class, "array", MapJsonReader.class);
MapArrayJsonReader map = json.fromJson(MapArrayJsonReader.class, Gdx.files.internal("assets/cfg/map.json"));
for(Object a : map.array){
MapJsonReader m = (MapJsonReader) a;
/* ... */
}