Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 936007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:14:44+00:00 2026-05-15T21:14:44+00:00

I know of some JSON libs around and I’m currently looking into Google-JSON but

  • 0

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.

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

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:14:44+00:00Added an answer on May 15, 2026 at 9:14 pm

    Install Google Gson and create those two model classes

    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:

    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:

    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().

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

    See also this answer for another complex Gson example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some JSON data, but it's all on one line. Does anyone know
I'm grabbing some JSON from YQL, parsing it into a list. Then I want
I know some SEO, but I have to ask: Is it unnecessary to add
I do know some basic differences but there are still some questions in my
i would like know some reference. I know i can googling it. but prefer
I have some JSON data coming from a service. I don't know the exact
I'm trying to get some json from an action but I have a problem.
I just want to save some JSON (generated with Javascript) in a file on
I don't know if this is possible or allowed, but basically I want to
Anyone know why some of my json elements are being backslash( \ ) escaped

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.