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

  • SEARCH
  • Home
  • 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 7755925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:41:15+00:00 2026-06-01T12:41:15+00:00

I’m having some trouble parsing a JSON that I obtain from my DB to

  • 0

I’m having some trouble parsing a JSON that I obtain from my DB to my Java (Android) Application. I hope you can help me out:

This is the Json that I have:

<br>
[{<br>
  "ID" : "1",<br>
  "name" : "Test name",<br>
  "type" : "1",<br>
  "Desc" : "blablabla",<br>
  "minNum" : "0",<br>
  "maxNum" : "12",<br>
  "Num" : "8",<br>
  "bool1" : "0",<br>
  "bool2" : "1",<br>
  "bool3" : "1",<br>
  "date" : "2012-04-01 23:00:00",<br>
  "double1" : "39.47208",<br>
  "doubl2" : "-0.3556063",<br>
  "someText" : "ajayeah",<br>
  "number" : "15",<br>
  "anotherNumber" : "1234"<br>
}, {"ID" : "2",<br>
  "name" : "Test name",<br>
  "type" : "1",<br>
  "Desc" : "blablabla",<br>
  "minNum" : "0",<br>
  "maxNum" : "12",<br>
  "Num" : "8",<br>
  "bool1" : "0",<br>
  "bool2" : "1",<br>
  "bool3" : "1",<br>
  "date" : "2012-04-01 23:00:00",<br>
  "double1" : "39.47208",<br>
  "doubl2" : "-0.3556063",<br>
  "someText" : "ajayeah",<br>
  "number" : "15",<br>
  "anotherNumber" : "1234"<br>
}]<br>

(The names are ovbiously not the ones I wrote here :P)

I have all that as a String (checked, OK), for example in a variable called responseString;

and then I’ve tried all possible ways to do the conversion but it always fails. I’ve tried to get it as an Object removing ‘[‘ ‘]’, as an Array (things I achieved but then this line fails):

MyClassList MyClas = new Gson().fromJson(responseString, MyClassList.class);

The error is:

com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY 

(with other tests converting it first to object or array, the error is with BEGIN_STRING)

Could it be because I’m using booleans, and Calendar type (in Date) and it’s not supported by Gson library?

Well, I hope you can get my out of here because I’m going mad.

Thanks 🙂


Don’t use Calendar, that was my problem. Instead, you may use Date and it works. Also, I don’t know why, booleans like “0”|”1″ don’t work. Like “true”|”false” yes.

  • 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-06-01T12:41:17+00:00Added an answer on June 1, 2026 at 12:41 pm

    The problem is that you are trying to deserialize an array into a class that is not an array, and that won’t work for obvious reasons. Since you know the array type just use any instance of MyClass[] to get the class from. You can create one at any time. Alternately pass in MyClass[].class (i.e MyClass.class is not the same as MyClass[].class)

    Here is an example that will run out of the box if you create a log4j.properties file (Or convert the loggers to System outs:

    package com.techtrip.test;
    
    import java.io.Serializable;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.google.gson.Gson;
    
    public class GsonTest {
    
        private static Logger logger = LoggerFactory.getLogger(GsonTest.class);
    
        static ToSerialize t1 = new ToSerialize("1", "Test 1");
        static ToSerialize t2 = new ToSerialize("2", "Test 2");
    
        static ToSerialize target[] = {t1,t2} ;
    
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Gson gson = new Gson(); 
    
            String jsonStr = gson.toJson(target);
    
            logger.info(String.format("Target As String\n: %s", jsonStr));
    
            // This will work as well --> ToSerialize test[] = gson.fromJson(jsonStr, target.getClass());
            ToSerialize test[] = gson.fromJson(jsonStr, ToSerialize[].class);
    
            for (ToSerialize deserialized: test){
                logger.info(String.format("From JSON\n: %s", deserialized.toString()));
            }
        }
    
    }
    
    class ToSerialize implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
    
        private String iD;
        private String name;
    
    
        public ToSerialize() {
            // TODO Auto-generated constructor stub
        }
    
        public ToSerialize(String iD, String name) {
            super();
            this.iD = iD;
            this.name = name;
        }
    
        public String getiD() {
            return iD;
        }
        public void setiD(String iD) {
            this.iD = iD;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((iD == null) ? 0 : iD.hashCode());
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            ToSerialize other = (ToSerialize) obj;
            if (iD == null) {
                if (other.iD != null)
                    return false;
            } else if (!iD.equals(other.iD))
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
    
        @Override
        public String toString() {
            return "ToSerialize [iD=" + iD + ", name=" + name + "]";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.