0

I am having one hell of a time trying to read a JSON object that is being passed from a URL from one my web pages. I need to be able to parse the data an insert into into our database. I have searched high and low all over this site as well as many others and still cannot get anything to work. Here is my last exhausted resource which still does not work.

Accessing members of items in a JSONArray with Java

Here is the object that is coming in.

{
 "tableData": [{
  "id": 1,
  "inspReq": "test1",
  "inspInit": "",
  "inspComm": "testing"
 }, {
  "id": 2,
  "inspReq": "test2",
  "inspInit": "bmb",
  "inspComm": "more%20testing"
 }, {
  "id": 3,
  "inspReq": "test3",
  "inspInit": "abc",
  "inspComm": "another%20test"
 }]
}

<%@ include file="faSite.jsp"%>

<%@ page import="org.json.simple.JSONObject"%>
<%@ page import="org.json.simple.JSONArray"%>
<%@ page import="org.json.simple.parser.ParseException"%>
<%@ page import="org.json.simple.parser.JSONParser"%>

<%

try{

 String json = request.getParameter("jsonData");
  
 JSONParser parser = new JSONParser();
 Object obj = parser.parse(json);
 
 System.out.println("created object");
 System.out.println(obj);
 
 JSONArray array = new JSONArray();
 array.add(obj);
 
 System.out.println("created array");
 System.out.println(array);
 
 for(Object o: array){
  
  System.out.println("inside JSON array");
  JSONObject jsonLineItem = (JSONObject) o;
  
  String inspReq = jsonLineItem.getString("inspReq");
       
 }
 
}catch(Exception e){
 e.printStackTrace();
}
 
%>

Here is my error.

500 Internal Server Error

Error parsing JSP page /production/FirstArticle/core/faPost.jsp Syntax error in source /production/FirstArticle/core/faPost.jsp.java:211: error: cannot find symbol (JSP page line 24)

    String inspReq = jsonLineItem.getString("inspReq");

                                 ^

symbol: method getString(String)

location: variable jsonLineItem of type JSONObject

Note: /production/FirstArticle/core/faPost.jsp.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

1 error

It is making it into the array and I am able to see the object on a log but once it tries to get the string, it fails.

Community
  • 1
  • 1
user3183411
  • 315
  • 2
  • 7
  • 19
  • 1
    Do yourself a favor, and put Java code in Java source files instead of JSPs. Then use your favorite IDE to compile the classes. JSP is a view technology. Its responsibility is to generate HTML. Not to parse JSON. You shouldn't have any Java code in JSPs. – JB Nizet Jun 24 '16 at 17:15
  • Thanks for the advice I will give that a try. – user3183411 Jun 24 '16 at 20:08
  • I did what you suggested but I am having a hard time finding some info on how to loop through an array. – user3183411 Jun 30 '16 at 17:16

0 Answers0