JSONException获取键值时发生错误。

huangapple go评论69阅读模式
英文:

JSONException getting the key value

问题

好的,以下是您提供的内容的中文翻译:

我的 "Students" 的 JSON 示例如下所示。

{
    "phone":"703-703-1234",
    "poc":"XYZ",
    "name":"ABC",
    "location":"California",
    "id":10,
    "deletedBy":null,
    "statusObj":{
        "descr":"IN PROGRESS",
        "id":10
    },
    "createDate":1595396946000,
    "deleteDate":null
}

用于获取子元素的代码如下:

JSONArray data = jsonObj.getJSONArray("Students");
for(int i=0; i<data.length(); i++){
    if(data.getJSONObject(i).has("statusObj") && !data.getJSONObject(i).isNull("statusObj")){
        JSONObject status = (JSONObject)data.getJSONObject(i).get("statusObj");
        Iterator iterator = status.keys();
        while(iterator.hasNext()){
            String key = (String)iterator.next();
            System.out.println("Descr is ..- "+key);
            //JSONObject page = status.getJSONObject(key);
            JSONObject page = status.getJSONObject("descr");
            System.out.println("Descr is - "+page);
        }
    }
}

当我尝试获取 "descr" 对象时,出现以下异常:

Descr is ..- descr
org.json.JSONException: JSONObject["descr"] is not a JSONObject.
	at org.json.JSONObject.getJSONObject(JSONObject.java:557)
英文:

My JSON for "Students" looks like the below example.

{
&quot;phone&quot;:&quot;703-703-1234&quot;,
&quot;poc&quot;:&quot;XYZ&quot;,
&quot;name&quot;:&quot;ABC&quot;,
&quot;location&quot;:&quot;California&quot;,
&quot;id&quot;:10,
&quot;deletedBy&quot;:null,
&quot;statusObj&quot;:{
      &quot;descr&quot;:&quot;IN PROGRESS&quot;,
      &quot;id&quot;:10},
&quot;createDate&quot;:1595396946000,
&quot;deleteDate&quot;:null}

My code to get the child elements is this:

JSONArray data = jsonObj.getJSONArray(&quot;Students&quot;);
for(int i=0; i&lt;data.length(); i++){
if(data.getJSONObject(i).has(&quot;statusObj&quot;) &amp;&amp; !data.getJSONObject(i).isNull(&quot;statusObj&quot;)){
	JSONObject status = (JSONObject)data.getJSONObject(i).get(&quot;statusObj&quot;);
	Iterator iterator = status.keys();
	while(iterator.hasNext()){
		String key = (String)iterator.next();
		System.out.println(&quot;Descr is ..- &quot;+key);
		//JSONObject page = status.getJSONObject(key);
		JSONObject page = status.getJSONObject(&quot;descr&quot;);
		System.out.println(&quot;Descr is - &quot;+page);
	}
}
}

I am getting the following exception when I try to get the "descr" obj

Descr is ..- descr
org.json.JSONException: JSONObject[&quot;descr&quot;] is not a JSONObject.
	at org.json.JSONObject.getJSONObject(JSONObject.java:557)

答案1

得分: 1

异常的含义非常明确 - 键&quot;descr&quot;不是指向一个对象,而是一个字符串。您应该使用getString代替:

String page = status.getString(&quot;descr&quot;);
英文:

The exception really says it all - the key &quot;descr&quot; doesn't refer to an object, but to a string. You should use getString instead:

String page = status.getString(&quot;descr&quot;);

huangapple
  • 本文由 发表于 2020年9月1日 03:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63677305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定