0

I am stuck here for two days, trying to read values of JSONobjects stored in JSONArray. I use JSON simple, its not helping out alot! I only can get to the JSONArray elemts that hold the JSONObjects by something like this jsonstring=JSONArrayName.get(indx); but then I cant read values from the JSON object stored in "jsonstring" string Please help!! please find my code below.

ps: I am using $.ajax, I need to store the values received and process/use it in my server

// here is my client side code Login.html


//My servlet code to process json received from client 
BufferedReader reader = request.getReader();
StringBuilder myinputholder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
    myinputholder.append(line);
}

Object obj = JSONValue.parse(myinputholder.toString());
JSONArray newjsonarr = (JSONArray) obj;
     //  JSONObject newjson= (JSONObject) newjsonarr.get(0); // this line causes errors 
PrintWriter pw = response.getWriter();
String f = JSONValue.toJSONString(newjsonarr.get(0));// this will give me a json object 
         // proper format but I cant do anything with the values inside

JSONValue.writeJSONString(f, pw); // this is only for troubleshooting 
TyJS
  • 1
  • 3
  • "this line causes errors" isn't very helpful. What error exactly? What is the value of myinputholder.toString()? Where did "list1" come from and what is it? If it isn't relevant to the question remove it from the code sample. – digitaljoel Jul 08 '14 at 18:17
  • I removed the list1, I previously tried to insert the array into a list, and I could but then I couldnt access the element, so thank you for this one. The error is basically whenever I keep this line, my application doesnt work sadly, so I am trying to get around this. problem. they myinputholder holds characters returned by the BufferReader, then I am taking the characters and converting them to a string in a StringBuilder – TyJS Jul 08 '14 at 18:46
  • Use a simpler library like java-json.jar. It has methods that would let you change the values, provided you know the JSON beforehand. If you can post a sample JSON, we might help you better. – jsjunkie Jul 08 '14 at 19:00
  • Ok, so you answered the list1 question, but skipped the others. What EXACTLY is the error you see. "Doesn't work" is not an error. Copy and paste an error message or stack trace. I already understand what _should_ be in myinputholder, but I want to know what is _actually_ in myinputholder. That should give us an example of the JSON you are trying to parse. Are you sure you have a value? Is it really an array? Does it contain any elements? Finally, as jsjunkie said, use a real library like jackson or gson. – digitaljoel Jul 08 '14 at 19:34
  • digitaljoel- I get server internal error. below is my ajax function data that iam passing the json array, I looked it up in the browser console, and it has been sending an array [{:"username": "d", "password": "8277e0910d750195b448797616e091ad"}]. here is what iam sending through my ajax function .. var d= []; d.push('{:"username": "' + usertemp + '", "password": "' + $.md5(passtemp) + '"}'); – TyJS Jul 08 '14 at 19:46
  • ... internal server error why? If it says "internal server error" then the webserver logs will have more information like a stack trace or something. The string you give is not valid json. You have a colon in front of "username" which shouldn't be there. replace d.push('{:"username": with d.push('{"username": notice the removal of the preceding colon. – digitaljoel Jul 08 '14 at 19:54
  • this is the exception : java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONObject – TyJS Jul 09 '14 at 15:38
  • I even tried this code, and still gave me the same exception stated above . code> for(Object realobj: newjsonarr){ JSONObject jo= (JSONObject) realobj; uname=jo.get("username").toString(); – TyJS Jul 09 '14 at 15:42

1 Answers1

0

I changed the library to Jackson and it worked prefectly fine. I had to make some tweaks to my String tho, I had to remove the quotes in the beginning of the string and the ones at the end "{}" then I had to replace all {} backslashes exist with space to get a valid string format { "Key": "Value", "Key":,"Value"} because my string looked like "{ \"Key\": \"Value\", \"Key\":,\"Value\"}\". I could extract each value of each key sent from the client side. find the code below.

    String uname="";
    String pass="";
    BufferedReader reader = request.getReader();
    StringBuilder myinputholder = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        myinputholder.append(line);
    }

    Integer s= myinputholder.length();

    String ss= myinputholder.toString();
    String sss= ss.substring(1, s-1); // this is to avoid the beginning and end "{}"
    sss=sss.replace("\\", ""); \\ this line is to replace all \ with space
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getJsonFactory();
    JsonParser jp = factory.createJsonParser(sss);
    JsonNode actualObj = mapper.readTree(jp);
    JsonNode subnode= actualObj.path("username");
    JsonNode subnode2= actualObj.path("password");
    uname= subnode.getTextValue();
    pass=actualObj.get("password").getTextValue();
TyJS
  • 1
  • 3