0

I was not able to convert String to JSONArray or JSONObject. Here is the code below:

JSONArray entries = WebRequest.execute(request);
if(entries!=null){          
    try{
        String temp  = entries.getJSONObject(0).getString(WebRequest.CONTENT);    
        String s = temp.toString();
        JSONArray cont = new JSONArray(s);

        Toast.makeText(getBaseContext(), cont.toString(), Toast.LENGTH_LONG).show();
    }catch(Exception e){                
    }
}

Here is the String result :

"[{\ID_PROJECT\":528,\"NM_PROJECT\":\"TestProject\",,\"NM_TASK\":\"TestTask\"}]"

I was not able to get the toast when using this code.

9
  • are you getting any exception? first change catch(Exception e) to catch(JSONException je) then try to find out exception Commented May 20, 2014 at 7:07
  • Add Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show(); in your catch block or even easier System.out.println(e.toString()) and please post the output Commented May 20, 2014 at 7:08
  • Spring edited my post with Json data Commented May 20, 2014 at 7:12
  • Its a invalid json buddy. check in jsonlint.com Commented May 20, 2014 at 7:14
  • 1
    this is right syntax of json{"data":[{"ID_PROJECT":528,"NM_PROJECT":"TestProject","NM_TASK":"TestTask"}]} Commented May 20, 2014 at 7:35

1 Answer 1

1

This is json parsing of your json string

String OutputData = "";
JSONObject jsonResponse;

try {

      /****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
      jsonResponse = new JSONObject("{\"data\":[{\"ID_PROJECT\":528,\"NM_PROJECT\":\"TestProject\",\"NM_TASK\":\"TestTask\"}]}");

      /***** Returns the value mapped by name if it exists and is a JSONArray. ***/
      /*******  Returns null otherwise.  *******/
      JSONArray jsonMainNode = jsonResponse.optJSONArray("data");

      /*********** Process each JSON Node ************/

      int lengthJsonArr = jsonMainNode.length();  

      for(int i=0; i < lengthJsonArr; i++) {
                    /****** Get Object for each JSON node.***********/
                    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

                    /******* Fetch node values **********/
                    int project_id        = Integer.parseInt(jsonChildNode.optString("ID_PROJECT").toString());
                    String project_name   = jsonChildNode.optString("NM_PROJECT").toString();
                    String task_name = jsonChildNode.optString("NM_TASK").toString();


                    OutputData += "Node : \n\n     "+ project_id +" | "
                                                    + project_name +" | "
                                                    + task_name +" \n\n ";

      }

      /************ Show Output on screen/activity **********/
      output.setText( OutputData );

  } catch (JSONException e) {

                e.printStackTrace();
            }

        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.