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 5993727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:42:57+00:00 2026-05-22T23:42:57+00:00

I need to parse a JSON item which could be defined as follows: {

  • 0

I need to parse a JSON item which could be defined as follows:

{
    ...
    "itemName": ""
    ...
}

or

{
    ...
    "itemName": {

    }
    ...
}

Basically, the feed i am reading from defines itemName as an empty string if there is no value, otherwise it is a regular JSON object which i can parse fine.

I believe this is what is causing the GSON error i am experiencing, although i may be wrong.

How can I parse such a JSON feed including fields defined like i have shown above, without causing a GSON error? Or how can i suppress this error and move on parsing?

here is the logcat:

ERROR/AndroidRuntime(32720): Caused by: com.google.gson.JsonParseException: Expecting object found: ""

I am using the GSON included in the AdMob 4.0.4 jar

  • 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-22T23:42:58+00:00Added an answer on May 22, 2026 at 11:42 pm

    You’d need to implement a custom deserializer for this. Following is one example.

    // output: 
    // {Outer: item=null}
    // {Outer: item={Inner: value=foo}}
    
    import java.lang.reflect.Type;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonParseException;
    
    public class Foo
    {
      static String json1 = "{\"item\":\"\"}";
      static String json2 = "{\"item\":{\"value\":\"foo\"}}";
    
      public static void main(String[] args)
      {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Inner.class, new InnerDeserializer());
        Gson gson = gsonBuilder.create();
        Outer outer1 = gson.fromJson(json1, Outer.class);
        System.out.println(outer1);
        Outer outer2 = gson.fromJson(json2, Outer.class);
        System.out.println(outer2);
      }
    }
    
    class InnerDeserializer implements JsonDeserializer<Inner>
    {
      @Override
      public Inner deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
          throws JsonParseException
      {
        if (json.isJsonPrimitive())
        {
          return null;
        }
        return new Gson().fromJson(json, typeOfT);
      }
    }
    
    class Outer
    {
      Inner item;
    
      @Override
      public String toString()
      {
        return String.format("{Outer: item=%s}", item);
      }
    }
    
    class Inner
    {
      String value;
    
      @Override
      public String toString()
      {
        return String.format("{Inner: value=%s}", value);
      }
    }
    

    This could easily be changed to return an empty Inner instance instead of null, depending on what’s desired, of course.

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

Sidebar

Related Questions

I'm working on a rake task which imports from a JSON feed into an
We have a complex json structure which we need to parse in Android. {
I get a json date from a webservice that i need to parse manualy
I need to parse a simple json response (from my rails app): [ {
I need to parse a xml file which is practically an image of a
I need to parse a log in the following format: ===== Item 5483/14800 =====
I’m new to android,I have an activity class which fetches Json string from url
I'm very new to JSON, and I need to parse some that an API
I need to parse a JSON Response that looks like: {key1: value1, key2: value2,
Given a JSON respond to a form I need to parse through the JSON

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.