读取以 [ 开头并以 ] 结尾的 JSON 数据

Reading JSON data that begins with [ and ends with ]

提问人:Hack-R 提问时间:3/13/2016 最后编辑:Victor OlexHack-R 更新时间:9/28/2016 访问量:1174

问:

我将 Java 与名为 CODAPPS 的 NetBeans 插件一起使用,以检索、解析和显示 JSON 数据。

我正在遵循一个使用 Google 的 Firebase 数据库生成的 JSON 数据的示例,但我使用的是 SlashDB。

出于某种原因,来自 SlashDB 的 JSON 数据分别以 和 开头和结尾。[]

我收到以下错误:

org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
    at org.json.JSONObject.<init>(JSONObject.java:198)
    at org.json.JSONObject.<init>(JSONObject.java:325)
    at userclasses.StateMachine.onMain_ButtonAction(StateMachine.java:80)
    at generated.StateMachineBase.handleComponentAction(StateMachineBase.java:572)
    at com.codename1.ui.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2831)
    at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:345)
    at com.codename1.ui.Button.fireActionEvent(Button.java:411)
    at com.codename1.ui.Button.released(Button.java:442)
    at com.codename1.ui.Button.pointerReleased(Button.java:530)
    at com.codename1.ui.Form.pointerReleased(Form.java:2578)
    at com.codename1.ui.Form.pointerReleased(Form.java:2514)
    at com.codename1.ui.Component.pointerReleased(Component.java:3119)
    at com.codename1.ui.Display.handleEvent(Display.java:2017)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1065)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:994)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
Picked up _JAVA_OPTIONS: -Xmx4G
BUILD SUCCESSFUL (total time: 15 seconds)

我的 JSON 数据如下所示:

[
    {
        "description": "Example Fast Festival Motorcycle with Boho Beading",
        "title": "Example Fast Festival Motorcycle with Boho Beading",
        "price": "$154.00",
        "pic": "http://images.example-media.com/inv/media/3/8/8/8/5068883/print/image1xxl.jpg",
        "pic2": "http://images.example-media.com/inv/media/3/8/8/8/5068883/image2s.jpg",
        "pic3": "http://images.example-media.com/inv/media/3/8/8/8/5068883/image3s.jpg",
        "title_lower": "example glam festival motorcycle with boho beading",
        "desc_lower": "example glam festival motorcycle with boho beading"
    },
    {
        "description": "Example BIG Fast Festival Body-Conscious Motorcycle with Cut Out Detail",
        "title": "Example BIG Fast Festival Body-Conscious Motorcycle with Cut Out Detail",
        "price": "$50.00",
        "pic": "http://images.example-media.com/inv/media/9/8/2/8/5118289/print/image1xxl.jpg",
        "pic2": "http://images.example-media.com/inv/media/9/8/2/8/5118289/image2s.jpg",
        "pic3": "http://images.example-media.com/inv/media/9/8/2/8/5118289/image3s.jpg",
        "title_lower": "example petite glam festival body-conscious motorcycle with cut out detail",
        "desc_lower": "example petite glam festival body-conscious motorcycle with cut out detail"
    },
    {
        "description": "Example USED Premium  Ultra Fast Fishtail Maxi Motorcycle",
        "title": "Example USED Premium  Ultra Fast Fishtail Maxi Motorcycle",
        "price": "$67.00",
        "pic": "http://images.example-media.com/inv/media/4/2/7/1/4571724/red/image1xxl.jpg",
        "pic2": "http://images.example-media.com/inv/media/4/2/7/1/4571724/image2s.jpg",
        "pic3": "http://images.example-media.com/inv/media/4/2/7/1/4571724/image3s.jpg",
        "title_lower": "example red carpet premium scuba ultra glam fishtail maxi motorcycle",
        "desc_lower": "example red carpet premium scuba ultra glam fishtail maxi motorcycle"
    }
]

引发错误的代码块是:

@Override
protected void onMain_ButtonAction(Component c, ActionEvent event) {
int pageNumber = 1;
ConnectionRequest r = new ConnectionRequest();
r.setUrl("http://myIP/query/basic_search/query/motorcycle.json?limit=41");
r.setPost(false);
r.setHttpMethod("GET");
r.setContentType("application/json");
NetworkManager.getInstance().addToQueueAndWait(r);

ByteArrayInputStream allSearchResultsInBytes = new ByteArrayInputStream(r.getResponseData());
String responseInString = null;        
try {
    responseInString = Util.readToString(allSearchResultsInBytes, "UTF-8");
} catch (IOException ex) {
    //Logger.getLogger(StateMachine.class.getName()).log(Level.SEVERE, null, ex);
}
JSONObject allSearchResultsAsJSON = new JSONObject(responseInString);
JSONArray listOfResultIds = allSearchResultsAsJSON.names();

Form wallScreen = c.getComponentForm();
Container myContainerForAllSearchResults = new Container();
Layout myLayout = new BoxLayout(BoxLayout.Y_AXIS);
myContainerForAllSearchResults.setLayout(myLayout);

Integer counter = 0;
while (counter < allSearchResultsAsJSON.length()) {
    String id = listOfResultIds.getString(counter);
    JSONObject oneSearchResultAsJSON = (JSONObject) allSearchResultsAsJSON.get(id);

    Container mySearchResultContainer = new Container();

    String motorcyclePrice = oneSearchResultAsJSON.getString("price");
    String motorcycleDesc  = oneSearchResultAsJSON.getString("description");
    String motorcycleTitle = oneSearchResultAsJSON.getString("title");
    String motorcyclePic   = oneSearchResultAsJSON.getString("pic");

    Label myLabelForPic   = new Label(motorcyclePic);
    Label myLabelForPrice = new Label(motorcyclePrice);
    Label myLabelForTitle = new Label(motorcycleTitle);
    Label myLabelForDesc  = new Label(motorcycleDesc);

    mySearchResultContainer.addComponent(myLabelForPrice);
    mySearchResultContainer.addComponent(myLabelForTitle);
    mySearchResultContainer.addComponent(myLabelForDesc);
    mySearchResultContainer.addComponent(myLabelForPic);

    myContainerForAllSearchResults.addComponent(mySearchResultContainer);

    counter = counter + 1;

}
    wallScreen.addComponent(wallScreen.getComponentCount(), myContainerForAllSearchResults);
    wallScreen.revalidate();

}
java json 代号一个 slashdb

评论


答:

7赞 Andreas 3/13/2016 #1

使用代替 .new JSONArray(responseInString)new JSONObject(responseInString)

评论

0赞 Hack-R 3/13/2016
太好了,有没有办法像我使用 JSONObject 方法所做的那样获取此类数组结果的索引?.names()
0赞 Hack-R 3/13/2016
我认为这是我在上面评论中问题的答案 stackoverflow.com/questions/7634518/......
3赞 selbie 3/13/2016 #2

输入字符串 (responseInstring) 是一个 JSON 数组,而不是 JSON 对象本身。

更改此行:

JSONObject allSearchResultsAsJSON = new JSONObject(responseInString);

要成为这个:

JSONArray allSearchResultsAsJSON = new JSONArray(responseInString);

并使用不同的索引方式,因为该方法仅适用于 JSONObject,而不是 JSONArray。allSearchResultsAsJSON.names