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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:33:48+00:00 2026-06-14T03:33:48+00:00

I have the following json notes: {note: [ { content: Having wisdom teeth removed.,

  • 0

I have the following json

"notes": {"note": [
         {
             "content": "Having wisdom teeth removed.",
             "from": "employee"
         },
         {
             "content": "Get well soon",
             "from": "manager"
         }
     ]},

the issue is that the value coud also be

 "notes": "",

or

"notes": {"note": {
            "content": "This is a test note.",
            "from": "employee"
        }},

and storing it in these

public  class Notes
{
    @SerializedName ("note")
    public List<Note> note;
}
public  class Note
{
    @SerializedName ("content")
    public String content;
    @SerializedName ("from")
    public String from;
}

I believe I solved the issue of not being an array but being an single object by doing this

public class Json {
    private static Gson gson;

    private static class MyNoteClassTypeAdapter implements JsonDeserializer<List<RequestsDTO.Note>> {
        public List<RequestsDTO.Note> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) {
            List<RequestsDTO.Note> vals = new ArrayList<RequestsDTO.Note>();
            if (json.isJsonArray()) {
                for (JsonElement e : json.getAsJsonArray()) {
                    vals.add((RequestsDTO.Note) ctx.deserialize(e, RequestsDTO.Note.class));
                }
            } else if (json.isJsonObject()) {
                vals.add((RequestsDTO.Note) ctx.deserialize(json,RequestsDTO.Note.class));
            } else {
                throw new RuntimeException("Unexpected JSON type: " + json.getClass());
            }
            return vals;
        }
    }

    public static Gson getGson()
    {
        if (gson == null)
        {
            Type ListType = new TypeToken<List<RequestsDTO.Note>>() {}.getType();
            GsonBuilder builder = new GsonBuilder();
            builder.registerTypeAdapter(DateTime.class, new DateTimeSerializer());
            builder.registerTypeAdapter(ListType, new MyNoteClassTypeAdapter());
            gson = builder.create();
        }
        return gson;
    }
}

And now I am stuck on when the whole thing just comes back as a string….

  • 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-14T03:33:49+00:00Added an answer on June 14, 2026 at 3:33 am

    The idea is try to get "note" field (from "notes" JSONObject) as JSONArray first and if it throws exception that will mean that there is no "note" JSONArray into "notes" JSONObject and that will mean that "note" is JSONObject. The same way we can figure out situation when note field is String.

    try {
            //String jsonString="{\"notes\": {\"note\": [{\"content\": \"Having wisdom teeth removed.\",\"from\": \"employee\" }, {\"content\": \"Get well soon\", \"from\": \"manager\"} ] }}";
            //String jsonString="{\"notes\": { \"note\": {\"content\": \"This is a test note.\",\"from\": \"employee\"}}}";
            String jsonString="{\"notes\": { \"note\": \"\"}}";
    
            JSONObject jsonObject=new JSONObject(jsonString);
            JSONObject jsonObjectNotes=jsonObject.getJSONObject("notes");
    
            try{
                JSONArray jsonArrayNote=jsonObjectNotes.getJSONArray("note");
                for (int i = 0; i < jsonArrayNote.length(); i++) {
    
                    JSONObject jsonObject2= jsonArrayNote.getJSONObject(i);
                    String stringContent=jsonObject2.getString( "content");
                    String stringFrom= jsonObject2.getString( "from");
    
                    Log.e(getClass().getName(), "content="+stringContent +"; from="+stringFrom);
                }
            }
            catch(JSONException e){
                //that means that jsonObjectNotes has no jsonArray with name "notes" and "notes" is jsonObject
                try{
                    JSONObject jsonObject3=jsonObjectNotes.getJSONObject("note");
    
                    String stringContent=(String) jsonObject3.get( "content");
                    String stringFrom=(String) jsonObject3.get( "from");
    
                    Log.e(getClass().getName(), "content="+stringContent +"; from="+stringFrom);
                }
                catch(JSONException ex){
                    //that means that jsonObjectNotes has no jsonObject with name "notes" and "notes" is empty String
                    String stringNote=jsonObjectNotes.getString("note") ;       
                    Log.e(getClass().getName(), "note is string ="+ stringNote);
                }
            }
    
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    In my example code another get operations can also throw jsonExceptions but I think you get the idea.

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

Sidebar

Related Questions

I have the following JSON string (from wikipedia http://en.wikipedia.org/wiki/JSON ) { name:Product, properties: {
I have the following json returned to me from an API. sections: { 1:
I have the following JSON coming back from a web service which I am
I have the following in the old syntax: render json: [@note.to_json(:include => { :contact
I have the following code which pulls json data from an ASP.NET page and
I have the following json object. var json1 = {00 : 00, 15 :
I have the following JSON String { name:Product, properties: { id: { type:number, description:Product
I have the following JSON structure: [{ id:10, class: child-of-9 }, { id: 11,
I have the following json code file named: sections.json { section1: { priority: 1,
I have the following JSON object: { response: { status: 200 }, messages: [

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.