I know of some JSON libs around and I'm currently looking into Google-JSON but all I want to achieve is something simple and I want to know what you would suggest.
我知道一些JSON列表,我目前正在研究Google-JSON,但我想要实现的只是一些简单的东西,我想知道您会建议什么。
I want a JSON library which will let me read a textfile that is in JSON and let me convert it into strings, int, boolean, etc. -- Now using Json.org/java
我想要一个JSON库,它可以让我读取JSON格式的textfile,并将其转换为string、int、boolean等等——现在使用Json.org/java
It can READ! BUT!!
它能读懂!但是! !
import org.json.*;
public class readJ {
public static String MapTitle;
public static int[][] tiles;
public static void main(String[] args) {
String json =
"{"
+"'name': 'map_one.txt',"
+"'title': 'Map One',"
+"'currentMap': 4,"
+"'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 ]]"
+"}";
try {
JSONObject JsonObj = new JSONObject(json);
MapTitle = JsonObj.getString("title");
tiles = JsonObj.getJSONArray("map");
}catch (JSONException er) {
er.printStackTrace();
}
System.out.println(MapTitle);
System.out.println(tiles[0][1]);
}
}
When compiling I get this error:
在编译时,我得到这个错误:
C:\Users\Dan\Documents\readJSON\readJ.java:32: incompatible types
found : org.json.JSONArray
required: int[][]
tiles = JsonObj.getJSONArray("map");
^
1 error
Tool completed with exit code 1
7 个解决方案
#1
4
Install Google Gson and create those two model classes
安装谷歌Gson并创建这两个模型类
public class Data {
private String name;
private String title;
private int currentMap;
private List<Item> items;
private int[][] map;
public String getName() { return name; }
public String getTitle() { return title; }
public int getCurrentMap() { return currentMap; }
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 setCurrentMap(int currentMap) { this.currentMap = currentMap; }
public void setItems(List<Item> items) { this.items = items; }
public void setMap(int[][] map) { this.map = map; }
}
and
和
public class Item {
private String name;
private int x;
private int y;
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; }
}
And convert your JSON as follows:
将JSON转换为:
Data data = new Gson().fromJson(json, Data.class);
To get the title just do:
要得到这个头衔,只需:
System.out.println(data.getTitle()); // Map One
And to get the map item at x=3 and y=3:
得到x=3和y=3处的映射项:
System.out.println(data.getMap()[3][3]); // 1
And to get the name of the first Item
:
得到第一个项目的名字:
System.out.println(data.getItems().get(0).getName()); // Pickaxe
Easy! Converting the other way on is also simple using Gson#toJson()
.
简单!使用Gson#到json()转换另一种方式也很简单。
String json = new Gson().toJson(data);
See also this answer for another complex Gson example.
关于另一个复杂的Gson示例,请参见此答案。
#2
3
I recommend this library: http://www.json.org/java/
我推荐这个库:http://www.json.org/java/。
You simply have to create a JSONObject from string, and get the name proprety.
您只需从字符串创建一个JSONObject,并获取名称proprety。
JSONObject JsonObj = JSONObject( InputStr );
String MapTitle = JsonObj.getString("title");
Download the source, and import it into your project: http://www.json.org/java/json.zip
下载源代码,并将其导入项目:http://www.json.org/java/json.zip
#3
1
The Spring Framework uses Jackson, so that's a fairly good endorsement for Jackson.
Spring Framework使用Jackson,所以这对Jackson来说是一个很好的支持。
JacksonInFiveMinutes
See the "Simple Data Binding Example" heading if you just want to use generic Maps.
如果您只想使用泛型映射,请参见“简单数据绑定示例”标题。
#4
1
As for the error messages.
至于错误消息。
C:\Users\Dan\Documents\readJSON\readJ.java:2: cannot find symbol
symbol : class json
location: package org
import org.json;
^
You don't usually name your package the same way the package you want to import, although you can.
您通常不会以希望导入的包的方式来命名包,尽管您可以这样做。
You have to either: 1 name it different, 2.- don't put the import
你必须做两件事:一件不同,二件不同。-不要输入
C:\Users\Dan\Documents\readJSON\readJ.java:27: cannot find symbol
symbol : method JSONObject(java.lang.String)
location: class org.json.readJ
JSONObject JsonObj = JSONObject(json);
You're missing a "new" there... it should be new JSONObject(...
你错过了一个新的……它应该是新的JSONObject(……)
#5
1
org.json.JSONException: Expected a ':' after a key at 148 [character 149 line 1]
org.json。JSONException:预期为“:”在一个关键字148[字符149行1]
Here, your json string is invalid:
这里,您的json字符串无效:
+ "'map': [ { 1,3,1,1,1,24,1,1,1,1,1,1,1 },"
That creates and array with objects inside, the first object has the attributes 1,3,1
etc without value.
它创建和数组中包含对象,第一个对象具有属性1、3、1等等,没有值。
Should be:
应该是:
+ "'map': [ [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],"
For that to be an array with arrays inside.
它是一个包含数组的数组。
Or
或
+ "'map': [ { 1:0,3:0,1:0,1:...
So you can have attributes 1,3,1 etc with value 0 but... that doesn't make sense
你可以有属性1 3 1等等值为0但是。这没有意义
#6
0
json-lib comes with an example of converting a String to a JSON Object:
JSON -lib提供了一个将字符串转换为JSON对象的示例:
http://json-lib.sourceforge.net/snippets.html#Creating_a_JSONObject_from_a_JSON_formatted_string
http://json-lib.sourceforge.net/snippets.html Creating_a_JSONObject_from_a_JSON_formatted_string
#7
0
You can do this just fine with google-gson. I think it'd look something like this:
你可以用google-gson做的很好。我想应该是这样的:
JsonParser parser = new JsonParser();
JsonObject object = parser.parse(text).getAsJsonObject();
String title = object.get("title").getAsString();
int currentMap = object.get("currentMap").getAsInt();
...
#1
4
Install Google Gson and create those two model classes
安装谷歌Gson并创建这两个模型类
public class Data {
private String name;
private String title;
private int currentMap;
private List<Item> items;
private int[][] map;
public String getName() { return name; }
public String getTitle() { return title; }
public int getCurrentMap() { return currentMap; }
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 setCurrentMap(int currentMap) { this.currentMap = currentMap; }
public void setItems(List<Item> items) { this.items = items; }
public void setMap(int[][] map) { this.map = map; }
}
and
和
public class Item {
private String name;
private int x;
private int y;
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; }
}
And convert your JSON as follows:
将JSON转换为:
Data data = new Gson().fromJson(json, Data.class);
To get the title just do:
要得到这个头衔,只需:
System.out.println(data.getTitle()); // Map One
And to get the map item at x=3 and y=3:
得到x=3和y=3处的映射项:
System.out.println(data.getMap()[3][3]); // 1
And to get the name of the first Item
:
得到第一个项目的名字:
System.out.println(data.getItems().get(0).getName()); // Pickaxe
Easy! Converting the other way on is also simple using Gson#toJson()
.
简单!使用Gson#到json()转换另一种方式也很简单。
String json = new Gson().toJson(data);
See also this answer for another complex Gson example.
关于另一个复杂的Gson示例,请参见此答案。
#2
3
I recommend this library: http://www.json.org/java/
我推荐这个库:http://www.json.org/java/。
You simply have to create a JSONObject from string, and get the name proprety.
您只需从字符串创建一个JSONObject,并获取名称proprety。
JSONObject JsonObj = JSONObject( InputStr );
String MapTitle = JsonObj.getString("title");
Download the source, and import it into your project: http://www.json.org/java/json.zip
下载源代码,并将其导入项目:http://www.json.org/java/json.zip
#3
1
The Spring Framework uses Jackson, so that's a fairly good endorsement for Jackson.
Spring Framework使用Jackson,所以这对Jackson来说是一个很好的支持。
JacksonInFiveMinutes
See the "Simple Data Binding Example" heading if you just want to use generic Maps.
如果您只想使用泛型映射,请参见“简单数据绑定示例”标题。
#4
1
As for the error messages.
至于错误消息。
C:\Users\Dan\Documents\readJSON\readJ.java:2: cannot find symbol
symbol : class json
location: package org
import org.json;
^
You don't usually name your package the same way the package you want to import, although you can.
您通常不会以希望导入的包的方式来命名包,尽管您可以这样做。
You have to either: 1 name it different, 2.- don't put the import
你必须做两件事:一件不同,二件不同。-不要输入
C:\Users\Dan\Documents\readJSON\readJ.java:27: cannot find symbol
symbol : method JSONObject(java.lang.String)
location: class org.json.readJ
JSONObject JsonObj = JSONObject(json);
You're missing a "new" there... it should be new JSONObject(...
你错过了一个新的……它应该是新的JSONObject(……)
#5
1
org.json.JSONException: Expected a ':' after a key at 148 [character 149 line 1]
org.json。JSONException:预期为“:”在一个关键字148[字符149行1]
Here, your json string is invalid:
这里,您的json字符串无效:
+ "'map': [ { 1,3,1,1,1,24,1,1,1,1,1,1,1 },"
That creates and array with objects inside, the first object has the attributes 1,3,1
etc without value.
它创建和数组中包含对象,第一个对象具有属性1、3、1等等,没有值。
Should be:
应该是:
+ "'map': [ [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],"
For that to be an array with arrays inside.
它是一个包含数组的数组。
Or
或
+ "'map': [ { 1:0,3:0,1:0,1:...
So you can have attributes 1,3,1 etc with value 0 but... that doesn't make sense
你可以有属性1 3 1等等值为0但是。这没有意义
#6
0
json-lib comes with an example of converting a String to a JSON Object:
JSON -lib提供了一个将字符串转换为JSON对象的示例:
http://json-lib.sourceforge.net/snippets.html#Creating_a_JSONObject_from_a_JSON_formatted_string
http://json-lib.sourceforge.net/snippets.html Creating_a_JSONObject_from_a_JSON_formatted_string
#7
0
You can do this just fine with google-gson. I think it'd look something like this:
你可以用google-gson做的很好。我想应该是这样的:
JsonParser parser = new JsonParser();
JsonObject object = parser.parse(text).getAsJsonObject();
String title = object.get("title").getAsString();
int currentMap = object.get("currentMap").getAsInt();
...