Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Passing Jagged Array to JSONoBJECT and StringEntity

    I am calling a .net webservice in java that would do a post to one of my client database. The method is expecting a sessoionId, and a jagged array. It also return a jagged array. I am having an issue passing the jagged array to the JSONStringer and StringEntity. Below is a simple of my code inside the doinbackground:
    XML Code:
    if(sessionId != "")
                {
                      URL = "http://10.0.2.2:88/Student/Grade";
                      requestPost = new HttpPost(URL);
                      requestPost.setHeader("Accept", "application/json");
                      requestPost.setHeader("Content-type", "application/json");
    
                      List<String[]> parameters = new ArrayList<String[]>();
                      parameters.add(new String[] {"StudentID","SSN"});
                      parameters.add(new String[] {"StudentLastName", "LastName"});
                      parameters.add(new String[] {"StudentGrade","Grade"});
    
                      JSONStringer VistAConnect = new JSONStringer()
                      .object()
                      .key("sessionId").value(sessionId)
    // I am not sure how to format the jaggedArray in here.
                      .key("JaggedArrayParameters").value(parameters)
                      .endObject();
    //Converting the below parameter to a string does not help either. 
                         StringEntity entity = new StringEntity(VistAConnect.toString());
                          requestPost.setEntity(entity);
                          httpClient = new DefaultHttpClient();
                          HttpResponse response1 = httpClient.execute(requestPost); 
                          HttpEntity responseEntity1 =  response1.getEntity(); 
    
                          char[] buffer1 = new char[(int)responseEntity1.getContentLength()];
                            InputStream stream1 =responseEntity1.getContent();
                            InputStreamReader reader1 = new InputStreamReader(stream1);
                            reader1.read(buffer1);
                            stream1.close();
    //when I look at the resultFromPost, it failed with the message String did not format properly.
                            resultFromPost= new String(buffer1);
    
                }
    Any help or suggestion would be greatly appreciated.

  2. #2
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Passing Jagged Array to JSONoBJECT and StringEntity

    Here is an example of the method expectation:
    [["Student"]["123"],["LastName"]["Smith"],["StudentGrade"]["A"]].

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default Re: Passing Jagged Array to JSONoBJECT and StringEntity

    Can I recommend using something like GSON?
    That way you don't have to hand code a JSON writer, you'd just use GSON's own JsonWriter.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Passing Jagged Array to JSONoBJECT and StringEntity

    Thank you. I am new to programming, so I am doing a lot of searching and trying to figure things out. I've never used GSON before, I'll have to see how to convert it.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default Re: Passing Jagged Array to JSONoBJECT and StringEntity

    Actually, I gave the wrong link there...should have been to the Gson page. But looking again at your stuff you would have to create an object that represented the data you wanted to send as Json. Which is no bad thing I suppose.

    Or use the example given in the above JSonWriter link to see how to structure hand carved code for doing this sort of thing, with methods handling different aspects of the object.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Jagged array loop question
    By Dankaru in forum New To Java
    Replies: 3
    Last Post: 11-14-2012, 03:21 PM
  2. passing a .txt into an array
    By j_rod722 in forum New To Java
    Replies: 6
    Last Post: 02-07-2011, 11:45 PM
  3. Passing Data From One Array To Another
    By Sly Cooper in forum New To Java
    Replies: 17
    Last Post: 01-24-2011, 06:07 AM
  4. Put JSONObject to JSONArray?
    By DigitalMan in forum Advanced Java
    Replies: 3
    Last Post: 04-03-2010, 09:04 AM
  5. Passing a 2D array
    By toymachiner62 in forum New To Java
    Replies: 6
    Last Post: 10-23-2009, 04:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •