从文本文件中读取JSON

我知道一些JSON库,我现在正在研究Google-JSON,但我想要实现的只是简单的事情,我想知道你会建议什么。

我想要一个JSON库,让我读取一个JSON文本文件,让我把它转换成字符串,int,boolean等。 – 现在使用Json.org/java

它可以阅读! 但!!

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

编译时我收到此错误:

 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 

安装Google Gson并创建这两个模型类

 public class Data { private String name; private String title; private int currentMap; private List items; private int[][] map; public String getName() { return name; } public String getTitle() { return title; } public int getCurrentMap() { return currentMap; } public List 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 items) { this.items = items; } public void setMap(int[][] map) { this.map = map; } } 

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

并转换您的JSON如下:

 Data data = new Gson().fromJson(json, Data.class); 

要获得标题,请执行以下操作:

 System.out.println(data.getTitle()); // Map One 

并在x = 3和y = 3处获取地图项:

 System.out.println(data.getMap()[3][3]); // 1 

并获取第一个Item的名称:

 System.out.println(data.getItems().get(0).getName()); // Pickaxe 

简单! 使用Gson#toJson()转换另一种方式也很简单。

 String json = new Gson().toJson(data); 

有关另一个复杂的Gson示例,请参阅此答案 。

我推荐这个库: http : //www.json.org/java/

您只需从字符串创建一个JSONObject,并获得名称proprety。

 JSONObject JsonObj = JSONObject( InputStr ); String MapTitle = JsonObj.getString("title"); 

下载源代码,并将其导入您的项目: http : //www.json.org/java/json.zip

Spring框架使用Jackson,因此对Jackson来说是一个相当不错的认可。

JacksonInFiveMinutes

如果您只想使用通用映射,请参阅“简单数据绑定示例”标题。

至于错误信息。

 C:\Users\Dan\Documents\readJSON\readJ.java:2: cannot find symbol symbol : class json location: package org import org.json; ^ 

您通常不会像要导入的包那样命名包,尽管可以。

您必须:1将其命名为不同,2 .-不要输入

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

你错过了一个“新”…它应该是new JSONObject(...

org.json.JSONException:在148处的某个键之后预期为’:'[字符149第1行]

这里,你的json字符串无效:

  + "'map': [ { 1,3,1,1,1,24,1,1,1,1,1,1,1 }," 

这创建了包含内部对象的数组,第一个对象具有属性1,3,1等没有值。

应该:

  + "'map': [ [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ]," 

这是一个内部有数组的数组。

要么

 + "'map': [ { 1:0,3:0,1:0,1:... 

所以你可以使用值为0的属性1,3,1等但是……这没有意义

json-lib附带了一个将String转换为JSON对象的示例:

http://json-lib.sourceforge.net/snippets.html#Creating_a_JSONObject_from_a_JSON_formatted_string

你可以用google-gson做到这一点。 我觉得它看起来像这样:

 JsonParser parser = new JsonParser(); JsonObject object = parser.parse(text).getAsJsonObject(); String title = object.get("title").getAsString(); int currentMap = object.get("currentMap").getAsInt(); ...