java - Reading JSON from a text file -


I know about some Jason Lib Libers and I am currently looking at Google-JSON, but I I want to achieve something simple and I want to know what you would suggest.

I want a JSON library that will allow me to read a text file that is in JSON and converts it to string, int, boolean etc. - now use Json.org/java

< P> Can read this! but !!

  Import org.json. *; Public class readJ {public static string mappil; Public static int [] [] tiles; Public static zero main (string [] args) {string jason = "{" + "'name': 'map_one.txt', '+' 'title': 'map one', '+' current size ': 4, "+" {Name: 'Bataksx', X: 2, y: 3} "+"], "+" '{': '' name: 'picax', x: 5, y: 1} "+" Map ": [[1,3,1,1,1,24,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,1,1,1,1, 24,1,1,1,1], "+" [1,7,7,7, 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, 24,1,1,1,1,1], "+" [1,1,3,1,1,24,1,1,1,1,1], "+" [1, 3,3,1,1,24,1,1,1,1,1]] "+"} "; Try {JSONObject JsonObj = New JSONObject (json); MapTitle = JsonObj.getString ("title"); Tiles = jasonobj.gatejesenare ("map"); } Hold (JasonXxation Er) {er.printStackTrace (); } System.out.println (MapTitle); Println (tiles [0] [1]); }}  

I get this error when compiling:

  c: \ user \ donation \ document \ readJSON \ readJ.java: 32: incompatible Type found: Org.json.JSONArray required: int [] [] Tiles = JsonObj.getJSONArray ("map"); ^ 1 error tool integer code 1  

and complete those two model sections Install

  public class data {private string name; Private string title; Private Ink Challuma; Private listing & lt; Items & gt; item; Private Ent [] [] Map; Public string getName () {return name; } Public string getTitle () {return title; } Get public intcontent () {return currentMap; } Public listing & lt; Items & gt; GetItems () {Return Items; } Public int [] [] getMap () {return map; } Public Zero Setname (string name) {this.name = name; } Public Zero Sattitles (String title) {this.title = title; } Public Zero Set Printmap (Ink Runup) {this.currentMap = currentMap; } Public Zero Settings (List & lt; Items & gt; Items) {this.items = items; } Public Zero Setmap (int [] [] Map) {this.map = map; }}  

and

  public class items {the name of the private string; Private int x; Private int y; Public string getName () {return name; } Public int getX () {return x; } Public int getY () {return y; } Public Zero Setname (string name) {this.name = name; } Public Zero setX (int x) {this.x = x; } Public zero set wai (int y) {this.y = y; }}  

and change your JSON as follows:

  Data Data = New GSN (). FromJson (json, Data.class);  

To get the title simply:

  System.out.println (data.getTitle ()); // map to a  

and to get the map item x = 3 and y = 3:

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

and to get the first item name :

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

Easy! Changing the other way Gson # toJson () .

  string jason = new gsn () Using Tosonson (Data) is also simpler;  

See also for another complex GSN example.


Comments