Exception Cast JsonObject

时间:2015-12-15 13:00:46

标签: java json

如何使用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返回给定网址的JSON,但效果很好,但错误在JSONArray jsonArray = (JSONArray)jsonobject;

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

我无法弄清楚我做错了什么。

1 个答案:

答案 0 :(得分:0)

尝试下面的一个

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"));
    }