Java JSON 数组文本文件

发布于 2024-10-16 04:13:57 字数 4709 浏览 2 评论 0原文

我已经看到了这一点。我似乎无法从文本文件中获取地图的数组。我的意思是,除了地图的数组图块之外,我可以获得其他所有内容。

这是文本文件:

{
'name': 'map_one.txt',
'title': 'xxx One',
'currentMap': 4,
'rightMap': 3,
'lefttMap': 5,
'downMap': 1,
'upMap': 2,
'items': [
    { name: 'Pickaxe', x: 5, y: 1 },
    { name: 'Battleaxe', x: 2, y: 3 }
],
    'map': [ [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],
    [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],
    [ 1,7,1,1,1,24,1,1,24,1,1,1,1 ],
    [ 1,7,1,1,7,1,1,1,24,1,1,1,1 ],
    [ 1,7,7,7,1,24,24,24,24,1,1,1,1 ],
    [ 1,1,7,1,1,24,1,24,1,1,1,1,1 ],
    [ 1,1,1,1,1,24,1,1,1,1,1,1,1 ],
    [ 1,1,3,1,1,24,1,1,1,1,1,1,1 ],
    [ 1,3,3,1,1,24,1,1,1,1,1,1,1 ]]
};

当我运行它时,我得到:

==========================
 JSON MAP LOAD...
==========================

Name of map: xxx One
File of map: map_one.txt
ID of map: 4

==========================
 ITEMS IN MAP
==========================

# OF ITEMS: 2

>> Name: Pickaxe (5, 1)
>> Name: Battleaxe (2, 3)

==========================
 TILES OF MAP
==========================

null
Press any key to continue . . .

看到空值了吗?它*应该是数组的数量。

我知道,我可能做错了。这是我到目前为止所拥有的:

import java.util.*;
import java.io.*;
import com.google.gson.*;


public class readGoogle {

    public static String MapTitle;
    public static Data data;
    public static Item item;
    public static String dan;
    public static FileReader fr;
    public static int number;
    public static int currentMap;
    public static int tile;
    public static String[] wepN;


        public static void main(String[] args) {


try {
    fr = new FileReader("map1.txt");
}catch(FileNotFoundException fne) {
    fne.printStackTrace();
}
        StringBuffer sb = new StringBuffer();
        char[] b = new char[1000];
        int n = 0;
        try {
        while ((n = fr.read(b)) > 0) {
             sb.append(b, 0, n);
         }
         }catch(IOException rex) {
             rex.printStackTrace();
         }
        String fileString = sb.toString();

    try {
    data = new Gson().fromJson(fileString, Data.class);
    }catch (Exception er) {
        er.printStackTrace();
    }

System.out.println("==========================\n JSON MAP LOAD...\n==========================\n");

    System.out.println("Name of map: " + data.getTitle());
    System.out.println("File of map: " + data.getName());
    System.out.println("ID of map: " + data.getCurrentMap());


String[] wepN = new String[100];
String[] wepX = new String[100];
String[] wepY = new String[100];
int[] tile = new int[256];

int wepQty = 0;

try {
for (int i=0; i < wepN.length; i++) {
    if (data.getItems().get(i).getName() == null || "".equals(data.getItems().get(i).getName())) {
        System.out.println(data.getItems().get(i).getName() + " -NO MOARE");
        break;
    }

    wepN[i] = data.getItems().get(i).getName();
    wepX[i] = Integer.toString(data.getItems().get(i).getX());
    wepY[i] = Integer.toString(data.getItems().get(i).getY());

    wepQty++;
}
}catch(Exception xe) { }

System.out.println("\n==========================\n ITEMS IN MAP\n==========================\n");
System.out.println("# OF ITEMS: " + wepQty + "\n");

for (int i=0; i < wepQty; i++) {
        System.out.println(">> Name: " + wepN[i] + " (" + wepX[i] + ", " + wepY[i] + ")");
    }

System.out.println("\n==========================\n TILES OF MAP\n==========================\n");

System.out.println(data.getMap());
    }


public static class Item {
        public String name;
        public int x;
        public int y;
        public int tile;

        public String getName() { return name; }
        public int getX() { return x; }
        public int getY() { return y; }

        public void setName(String name) { this.name = name; }
        public void setX(int x) { this.x = x; }
        public void setY(int y) { this.y = y; }
    }


      public static class Data {
            private String name;
            private String title;
            private int currentMap;
            private List<Item> items;
            private int[][] tile;

            public String getName() { return name; }
            public int getCurrentMap() { return currentMap; }
            public String getTitle() { return title; }
            public List<Item> getItems() { return items; }
            public int[][] getMap() { return tile; }

            public void setName(String name) { this.name = name; }
            public void setTitle(String title) { this.title = title; }
            public void setItems(List<Item> items) { this.items = items; }
            public void setMap(int[][] tile) { this.tile = tile; }
        }

}

I've seen to hit a bump with this. I can't seem to get the array of the map from text file. I mean, I can get everything else BUT the map's array tiles.

This is the text file:

{
'name': 'map_one.txt',
'title': 'xxx One',
'currentMap': 4,
'rightMap': 3,
'lefttMap': 5,
'downMap': 1,
'upMap': 2,
'items': [
    { name: 'Pickaxe', x: 5, y: 1 },
    { name: 'Battleaxe', x: 2, y: 3 }
],
    'map': [ [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],
    [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],
    [ 1,7,1,1,1,24,1,1,24,1,1,1,1 ],
    [ 1,7,1,1,7,1,1,1,24,1,1,1,1 ],
    [ 1,7,7,7,1,24,24,24,24,1,1,1,1 ],
    [ 1,1,7,1,1,24,1,24,1,1,1,1,1 ],
    [ 1,1,1,1,1,24,1,1,1,1,1,1,1 ],
    [ 1,1,3,1,1,24,1,1,1,1,1,1,1 ],
    [ 1,3,3,1,1,24,1,1,1,1,1,1,1 ]]
};

and when I run it, I get this:

==========================
 JSON MAP LOAD...
==========================

Name of map: xxx One
File of map: map_one.txt
ID of map: 4

==========================
 ITEMS IN MAP
==========================

# OF ITEMS: 2

>> Name: Pickaxe (5, 1)
>> Name: Battleaxe (2, 3)

==========================
 TILES OF MAP
==========================

null
Press any key to continue . . .

See the null? It's *suppose to be numbers of arrays.

I'm doing it wrong, probably, I know. Here's what I have so far:

import java.util.*;
import java.io.*;
import com.google.gson.*;


public class readGoogle {

    public static String MapTitle;
    public static Data data;
    public static Item item;
    public static String dan;
    public static FileReader fr;
    public static int number;
    public static int currentMap;
    public static int tile;
    public static String[] wepN;


        public static void main(String[] args) {


try {
    fr = new FileReader("map1.txt");
}catch(FileNotFoundException fne) {
    fne.printStackTrace();
}
        StringBuffer sb = new StringBuffer();
        char[] b = new char[1000];
        int n = 0;
        try {
        while ((n = fr.read(b)) > 0) {
             sb.append(b, 0, n);
         }
         }catch(IOException rex) {
             rex.printStackTrace();
         }
        String fileString = sb.toString();

    try {
    data = new Gson().fromJson(fileString, Data.class);
    }catch (Exception er) {
        er.printStackTrace();
    }

System.out.println("==========================\n JSON MAP LOAD...\n==========================\n");

    System.out.println("Name of map: " + data.getTitle());
    System.out.println("File of map: " + data.getName());
    System.out.println("ID of map: " + data.getCurrentMap());


String[] wepN = new String[100];
String[] wepX = new String[100];
String[] wepY = new String[100];
int[] tile = new int[256];

int wepQty = 0;

try {
for (int i=0; i < wepN.length; i++) {
    if (data.getItems().get(i).getName() == null || "".equals(data.getItems().get(i).getName())) {
        System.out.println(data.getItems().get(i).getName() + " -NO MOARE");
        break;
    }

    wepN[i] = data.getItems().get(i).getName();
    wepX[i] = Integer.toString(data.getItems().get(i).getX());
    wepY[i] = Integer.toString(data.getItems().get(i).getY());

    wepQty++;
}
}catch(Exception xe) { }

System.out.println("\n==========================\n ITEMS IN MAP\n==========================\n");
System.out.println("# OF ITEMS: " + wepQty + "\n");

for (int i=0; i < wepQty; i++) {
        System.out.println(">> Name: " + wepN[i] + " (" + wepX[i] + ", " + wepY[i] + ")");
    }

System.out.println("\n==========================\n TILES OF MAP\n==========================\n");

System.out.println(data.getMap());
    }


public static class Item {
        public String name;
        public int x;
        public int y;
        public int tile;

        public String getName() { return name; }
        public int getX() { return x; }
        public int getY() { return y; }

        public void setName(String name) { this.name = name; }
        public void setX(int x) { this.x = x; }
        public void setY(int y) { this.y = y; }
    }


      public static class Data {
            private String name;
            private String title;
            private int currentMap;
            private List<Item> items;
            private int[][] tile;

            public String getName() { return name; }
            public int getCurrentMap() { return currentMap; }
            public String getTitle() { return title; }
            public List<Item> getItems() { return items; }
            public int[][] getMap() { return tile; }

            public void setName(String name) { this.name = name; }
            public void setTitle(String title) { this.title = title; }
            public void setItems(List<Item> items) { this.items = items; }
            public void setMap(int[][] tile) { this.tile = tile; }
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2024-10-23 04:13:57

我的想法是,Data 类有一个tiles 字段来保存地图,但在 JSON 中它被命名为map。

尝试:

public static class Data {
            private String name;
            private String title;
            private int currentMap;
            private List<Item> items;
            private int[][] map;

            public String getName() { return name; }
            public int getCurrentMap() { return currentMap; }
            public String getTitle() { return title; }
            public List<Item> getItems() { return items; }
            public int[][] getMap() { return map; }

            public void setName(String name) { this.name = name; }
            public void setTitle(String title) { this.title = title; }
            public void setItems(List<Item> items) { this.items = items; }
            public void setMap(int[][] map) { this.map=  map; }
        }

My thought is that the Data class has a tiles field for holding the map but in the JSON it is named map.

try:

public static class Data {
            private String name;
            private String title;
            private int currentMap;
            private List<Item> items;
            private int[][] map;

            public String getName() { return name; }
            public int getCurrentMap() { return currentMap; }
            public String getTitle() { return title; }
            public List<Item> getItems() { return items; }
            public int[][] getMap() { return map; }

            public void setName(String name) { this.name = name; }
            public void setTitle(String title) { this.title = title; }
            public void setItems(List<Item> items) { this.items = items; }
            public void setMap(int[][] map) { this.map=  map; }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文