English 中文(简体)
JSONObject
原标题:JSONObject when element doesn t exist

I have a struts action receiving following JSON:

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

法典颁布后,仅罚款:

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

由于def>可以是空洞的,不是宣布的,或者可以包含其他一系列内容。 我试图这样做:

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

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

这只有def。 例如:

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

def 是空的或没有界定我在以下线上工作的方案站:JSONObject def = 指挥.getJSONObject(`def');,并注意到它没有继续执行?

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?

我期望它归还一个空洞的JSONObject,并继续处决。

我想检查一下的是,是否在def中界定了任何内容,然后,如果是的话,则根据在我的节目中发现的价值决定什么。 如果一个客户的 j在def空洞或没有界定的情况下,我就找不到一种办法使我的方案工作。

最佳回答

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.

问题回答

与此类似,是因为它试图找到一个目标并找到一种扼杀。 在您的JSON(如果你控制的话)中,对于一个空物体,I将填写{>>>>>>。 这将使 Java能够认为它正在检索物体。

If def is intended to be an object is it not suppose to look like this when empty?

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

I think having "def":"" will cause the value to be attempted to be parsed as a string value and not an object value.

也许这将有助于人。 我不得不解决同样的问题。 就我而言,如果网络服务无法找到所要求的记录,网络服务就正在退回空洞的JSON物体。

Note: the data names have been changed to protect the innocent... Note 2: this example uses javax.json

import javax.json.*;

JsonObject _jObj = _myRootObj.getJsonObject("someDataNode");

// at this point in execution _jObj could equal {"DataReturn":""}       
// or {"DataReturn":"<some valid data>"}
// we want to test the existence of DataReturn before trying to 
// use it

JsonValue jv = _jObj.getOrDefault("DataReturn", null);
String js = jv.toString(); 

// cover all the bases. test the value
if (js == null || js.isEmpty() || js.compareTo("""") == 0)
{
    throw new Exception("Error: DataReturn object null. Cannot proceed.");
}

// the object exists, so it s ok to get it
JsonObject jObjDate = _jObj.getJsonObject("DataReturn");

If you re using org.json.JSONObject you can use .isNull(String key) to do this, something like:

if (command.isNull("def") {
    // Handle  def  not being there
} else {
    JSONObject def = command.getJSONObject("def");
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...