gpt4 book ai didi

java - 异常转换 JsonObject

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:24 26 4
gpt4 key购买 nike

如何使用 JsonObject 和 JsonArray 读取此文件?我怎样才能检索 typeId值?

    {
"mockup": {
"controls": {
"control": [
{
"ID": "5",
"measuredH": "400",
"measuredW": "450",
"properties": {
"bold": "true",
"bottomheight": "0",
"italic": "true",
"size": "20",
"text": "Test",
"topheight": "26",
"underline": "true",
"verticalScrollbar": "true"
},
"typeID": "TitleWindow",
"x": "50",
"y": "50",
"zOrder": "0"
},
{
"ID": "6",
"measuredH": "27",
"measuredW": "75",
"properties": {
"align": "left",
"bold": "true",
"color": "0",
"italic": "true",
"menuIcon": "true",
"size": "18",
"state": "selected",
"text": "OK",
"underline": "true"
},
"typeID": "Button",
"x": "67",
"y": "85",
"zOrder": "1"
}
]
},
"measuredH": "450",
"measuredW": "500",
"mockupH": "400",
"mockupW": "450",
"version": "1.0"
}
}

我正在使用此代码:

import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {

private static final String _strDesktopDirectory = System.getProperty("user.home") + "/Desktop";

public static void main(String[] args) {

JSONParser _jspMyJsonParser = new JSONParser();

JFileChooser _fcMyFileChooser = new JFileChooser("Open JSON File");

FileNameExtensionFilter _fneJsonFilter = new FileNameExtensionFilter("JSON Files (*.json)", "json");

_fcMyFileChooser.setFileFilter(_fneJsonFilter);

int _iReturnFile = _fcMyFileChooser.showOpenDialog(_fcMyFileChooser);

_fcMyFileChooser.setCurrentDirectory(new File(_strDesktopDirectory));

if (_iReturnFile == JFileChooser.APPROVE_OPTION) {

try {


String _strSelectedFile = _fcMyFileChooser.getSelectedFile().toString();


Object _oMyObject = _jspMyJsonParser.parse(new FileReader(_strSelectedFile));


// Exception Here.
JSONObject _jsnoMockup = (JSONObject) _oMyObject;


_jsnoMockup = (JSONObject) _jsnoMockup.get("mockup");
JSONObject _jsnoControls = (JSONObject) _jsnoMockup.get("controls");


System.out.println("Mockup: " + _jsnoMockup);
System.out.println("Controls: " + _jsnoControls);


} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}


} else {

System.out.println("Close");
System.exit(0);

}

}
}

错误是:

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.JSONObject

注意:我没有任何 Java 经验。方法getJSONfromURL返回给定 URL 的 JSON,工作正常,但错误位于 JSONArray jsonArray = (JSONArray)jsonobject;

它给出以下错误:cannot cast JSONObject to JSONArray 。我也尝试过这个:JSONArray jsonArray = (JSONObject)(JSONArray)jsonobject;

我不知道我做错了什么。

最佳答案

尝试下面的一个

JSONArray jsonArray = new JSONArray();
jsonArray = jsonObject.getJSONObject("mockup").getJSONObject("controls").getJSONArray("control");
for(i=0;i<jsonArray.lenght();i++){
System.out.println(jsonArray.getJSONObject(i).getString("typeID"));
}

关于java - 异常转换 JsonObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34289966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com