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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:10:48+00:00 2026-05-28T02:10:48+00:00

I have a struts action receiving following JSON: { commandId:tC, id:123, def: } Following

  • 0

I have a struts action receiving following JSON:

{             
"commandId":"tC",       
"id":"123", 
"def":""
}

Following code works just fine:

JSONObject command = null;
String commandId = null;
String deviceId = null;
try {
  command = new JSONObject(json);
  commandId = command.getString("commandId");
}

Since “def” can be empty, non declared or can contain another array of elements I tried doing this:

JSONObject def = command.getJSONObject("def");

in order to get this JSON object defined in the element def

This only works if def isn’t empty like in this example:

{             
    "commandId":"tC",       
    "id":"123", 
    "def":{"1":"aaa", "2":"bbb"}
}

When def is empty or not defined my program stops working on the line JSONObject def = command.getJSONObject("def"); and noticed that it doesn’t continue the execution?!

If I put JSONObject def = command.getJSONObject("def"); try / catch block I get _JSONObject[“def”] is not a JSONObject _ exception, but execution doesn’t continue

How does JSONObject.getJsonObject(String) behave?

I would expect it to return an empty JSONObject and continue the execution.

What I want is to check if there is anything defined in def and then in a if, else decide what to do in my program according to the value found there… I can’t find a way to make my program work if a client’s json comes with def empty or not defined.

  • 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-28T02:10:49+00:00Added an answer on May 28, 2026 at 2:10 am

    my suggestion is to define “def” either be defined as null or {}:
    “def”:null or “def”:{} to align with its usage agreement

    quotes is really just used to indicate the value is a string. following the standard might save you and others from confusion in the future.

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

Sidebar

Related Questions

I have a Struts 2 action returning a JSON result. {columns:[coupon,CM,CM+1,CM+2,CM+3],couponList:[{coupon:3.0,CM:88.2323,CM+1:89.45,CM+2:132.3128125,CM+3:32.82},{coupon:3.5,CM:25,CM+1:3125,CM+2:333,CM+3:5}],Caption:30 Yr Fixed} I
I have a Struts 2 jsp with the following code... <s:iterator value=categories id=category status=iteratorStatus>
I'm using Spring and struts and have the following entry in '/META-INF/context.xml' <Context cachingAllowed=false
I have a html form with action attribute pointing to struts action Here is
I want to generate xml file from Struts action object. So that I have
In Struts 1 you could have, in struts-config.xml, a declaration like: <action path=/first forward=/second.do>
I have an input in Struts 2 form like this: <s:form action=Action> <input type=text
I have a problem with authorized SSL connection. I have created Struts Action that
I'm using Struts 1. I have an Action, and when this Action forwards to
I have Struts 2 configured to redirect any java.lang.Exception to a special Action which

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.