从 Json 获取字符串?

发布于 2024-11-27 01:43:59 字数 1668 浏览 2 评论 0原文

我的 Android 应用程序有一个奇怪的问题。在一种方法中,我这样做:

try {
 String r = responseBody.toString();
 JSONArray jArray = new JSONArray(r);
 categorys = new String[jArray.length()];
 idcategory = new Integer[jArray.length()];
 System.out.println("lung " + jArray.length());
  for (int i = 0; i < jArray.length(); i++) {
    JSONObject jsdata = jArray.getJSONObject(i);
    String idcat = jsdata.getString("id_category");
    idcategory[i] = Integer.valueOf(idcat);
    System.out.println("Id " + idcat);
    String namecategory = jsdata.getString("category_name");
    categorys[i] = namecategory;
    System.out.println("Category name  " + namecategory);
}

一切正常,我从服务器获取类别和类别的 id。在另一种方法中,我这样做(为了其他响应):

try {
 String re = responseBody.toString();
 JSONArray jArray = new JSONArray(re);
 System.out.println("lung " + jArray.length());
 titlephotos = new String[jArray.length()];
 photolink=new String[jArray.length()];
 for (int i = 0; i < jArray.length(); i++) {
   JSONObject jsdata = jArray.getJSONObject(i);
   String titlephoto = jsdata.getString("title");                       
   System.out.println("titlu photo " + titlephoto);
   titlephotos[i] = titlephoto;

   String linkphoto=jsdata.getString("view");
   System.out.println("link photo"+ linkphoto);
   photolink[i]=linkphoto;
 }
}catch(JSONException e) {
  System.out.println("You are in catch");
}

我只得到一张标题照片,然后我从 catch() 收到消息。如果我不放的话

String linkphoto=jsdata.getString("photo link");
System.out.println("link photo"+ linkphoto);
photolink[i]=linkphoto;

我会得到所有的头衔。我不明白问题出在哪里,因为方法相似,第一个工作正常。有人可以帮忙吗?

谢谢...

I have a strange problem in my android app. In one method I do this :

try {
 String r = responseBody.toString();
 JSONArray jArray = new JSONArray(r);
 categorys = new String[jArray.length()];
 idcategory = new Integer[jArray.length()];
 System.out.println("lung " + jArray.length());
  for (int i = 0; i < jArray.length(); i++) {
    JSONObject jsdata = jArray.getJSONObject(i);
    String idcat = jsdata.getString("id_category");
    idcategory[i] = Integer.valueOf(idcat);
    System.out.println("Id " + idcat);
    String namecategory = jsdata.getString("category_name");
    categorys[i] = namecategory;
    System.out.println("Category name  " + namecategory);
}

and everything works fine, I get from server the categorys and category's id. In an other method I do this (for an other response) :

try {
 String re = responseBody.toString();
 JSONArray jArray = new JSONArray(re);
 System.out.println("lung " + jArray.length());
 titlephotos = new String[jArray.length()];
 photolink=new String[jArray.length()];
 for (int i = 0; i < jArray.length(); i++) {
   JSONObject jsdata = jArray.getJSONObject(i);
   String titlephoto = jsdata.getString("title");                       
   System.out.println("titlu photo " + titlephoto);
   titlephotos[i] = titlephoto;

   String linkphoto=jsdata.getString("view");
   System.out.println("link photo"+ linkphoto);
   photolink[i]=linkphoto;
 }
}catch(JSONException e) {
  System.out.println("You are in catch");
}

and I get only one title photo an after that I get the message from catch(). If I don't put

String linkphoto=jsdata.getString("photo link");
System.out.println("link photo"+ linkphoto);
photolink[i]=linkphoto;

I get all the titles. I don't understand where is the problem,because the methods are similar, and the first one works fine. Can anyone help?

Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2024-12-04 01:43:59

最有可能的是其中的空间
String linkphoto=jsdata.getString("照片链接");

Most likely it is the space in
String linkphoto=jsdata.getString("photo link");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文