【问题标题】:Passing Jagged Array to JSONoBJECT and StringEntity将锯齿状数组传递给 JSONoBJECT 和 StringEntity
【发布时间】:2013-04-30 12:34:57
【问题描述】:

我正在调用一个 Web 服务,它会向我的一个客户端数据库发布一个帖子。该方法需要一个 sessionId 和一个锯齿状数组。它还返回一个锯齿状数组。我在将锯齿状数组传递给 JSONStringer 和 StringEntity 时遇到问题。下面是我在 doinbackground 中的一个简单代码:

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)

//这里不知道怎么格式化jaggedArray。

                  .key("JaggedArrayParameters").value(parameters)
                  .endObject();

//将下面的参数转换为字符串也无济于事。

                     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();

//当我查看resultFromPost时,它失败了,消息String没有正确格式化。

                        resultFromPost= new String(buffer1);

            }

任何帮助或建议将不胜感激。

【问题讨论】:

  • 在此上下文中定义锯齿状数组,以示例
  • 期待 student[1][0] = "StudentID" 和 student[1][1] = "543"
  • 这里有一个更好的例子 [["Student"]["123"],["LastName"]["Smith"],["StudentGrade"]["A"]]。跨度>
  • 您的示例不是有效的 json。 [["Student"],["123"],["LastName"], ... 将是,或 [[["Student"],["123"]],[["LastName"], .. .
  • 这可能是由于我有限的编程经验,但我不知道如何传递参数中的值 StringEntity(parameters) parameters = new ArrayList(); parameters.add(new String[] {"StudentID","SSN"}); parameters.add(new String[] {"StudentLastName", "LastName"}); parameters.add(new String[] {"StudentGrade","Grade"});

标签: java android jagged-arrays


【解决方案1】:

参数必须使用 JSONArray 而不是

List<String> or String[][] = {new [] String{"example","text"}
};
List<String[]> parameters = new ArrayList<String[]>();
parameters.add(new String[] {"StudentID","SSN"});
parameters.add(new String[] {"StudentLastName", "LastName"});
parameters.add(new String[] {"StudentGrade","Grade"});

改用这个:

JSONArray jArray= new JSONArray();
jArray.put(0, "example");
jArray.put(1, "Text");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2020-04-02
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多