Java 读取以[开头,以]结尾的JSON数据
我使用Java和一个名为CODAPPS的NetBeans插件来检索、解析和显示JSON数据 我下面的例子使用了谷歌Firebase数据库生成的JSON数据,但我使用的是SlashDB 出于某种原因,SlashDB中的JSON数据分别以Java 读取以[开头,以]结尾的JSON数据,java,json,codenameone,slashdb,Java,Json,Codenameone,Slashdb,我使用Java和一个名为CODAPPS的NetBeans插件来检索、解析和显示JSON数据 我下面的例子使用了谷歌Firebase数据库生成的JSON数据,但我使用的是SlashDB 出于某种原因,SlashDB中的JSON数据分别以[和]开头和结尾 我得到以下错误: org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1] at org.json.JSONTokener
[
和]
开头和结尾
我得到以下错误:
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)
引发错误的代码块是:
@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();
}
@覆盖
主按钮操作上的受保护无效(组件c,动作事件){
int pageNumber=1;
ConnectionRequest r=新的ConnectionRequest();
r、 setUrl(“http://myIP/query/basic_search/query/motorcycle.json?limit=41");
r、 setPost(假);
r、 setHttpMethod(“GET”);
r、 setContentType(“应用程序/json”);
NetworkManager.getInstance().addToQueueAndWait(r);
ByteArrayInputStream allSearchResultsInBytes=新建ByteArrayInputStream(r.getResponseData());
字符串响应安装=null;
试一试{
responseInstalling=Util.readToString(allSearchResultsInBytes,“UTF-8”);
}捕获(IOEX异常){
//Logger.getLogger(StateMachine.class.getName()).log(Level.SEVERE,null,ex);
}
JSONObject allSearchResultsAsJSON=新JSONObject(响应安装);
JSONArray ListofResultId=allSearchResultsAsJSON.names();
Form wallScreen=c.getComponentForm();
容器myContainerForAllSearchResults=新容器();
布局myLayout=新的BoxLayout(BoxLayout.Y_轴);
myContainerForAllSearchResults.setLayout(myLayout);
整数计数器=0;
while(计数器
使用新JSONArray(responseInstalling)
而不是新JSONObject(responseInstalling)
您的输入字符串(responseInstalling)是一个JSON数组,而不是JSON对象本身
更改此行:
JSONObject allSearchResultsAsJSON = new JSONObject(responseInString);
为此:
JSONArray allSearchResultsAsJSON = new JSONArray(responseInString);
并使用另一种索引
所有SearchResultsAsJSON
的方法,因为.names
方法仅适用于JSONObject,而不适用于JSONArray 很好,有没有一种方法可以像我使用JSONObject的.names()
方法那样获取这样一个数组的结果的索引?我想这就是我在上面的评论中提出的问题的答案
JSONArray allSearchResultsAsJSON = new JSONArray(responseInString);