繁体   English   中英

JSON解析为ListView Android

[英]JSON parsing into ListView Android

这是我通过解析我的URL获得的JsonObject: {"status":0,"items":"1333333454510|-7611868|-7222457"

现在我需要seperetly以获得单独编号long

如果我尝试将其转换为字符串,也会收到错误消息: org.json.JSONException: Value 1333333454510|-7611868|-7222457 at items of type java.lang.String cannot be converted to JSONObject

怎么样? 谢谢

这是一些代码

JSONObject obj = JSONParser.getJSONFromUrl(URL + "search", parameters);
String firstItem = obj.getJSONObject("items").toString();

使用\\\\|分割items

范例:

String items= "1333333454510|-7611868|-7222457" ; //here you got items

String[] _items=items.split("\\|");

现在,使用Long.ParseLong().迭代循环并将其转换为long Long.ParseLong().

编辑:

正如Ashwin所说,您需要使用json.getString("items"); 获取物品。

您可以使用以下代码将其转换为long。

ArrayList<Long> longs=new ArrayList<Long>();


for(int i=0;i<_items.length;i++){
longs.add(Long.parseLong(_items[i]));
}

使用拆分功能进行分隔。

解析json以获取项目String中的项目

使用jsonObject.getString而不是jsonObject.getJSONObject以避免错误。

String items= "1333333454510|-7611868|-7222457" ; 
String[] itemsArray =items.split("\\|");
String response= "{"status":0,"items":"1333333454510|-7611868|-7222457"}" ; // ie, response contains the string.

JsonObject json= new JsonObject(response);

String item=json.getString("items");
String [] items =item.split("|");

将在items数组中获得3个数字。

你的问题在这里

  JSONObject obj = JSONParser.getJSONFromUrl(URL + "search", parameters);
  String firstItem = obj.getJSONObject("items").toString();

所以将其更改为

String firstItem = obj.getString("items").toString();

然后按照其他人的建议进行拆分操作。

所以总是与你在一起:)

JsonObject json= new JsonObject(response);

String item=json.getString("items");
String [] items =item.split("|");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM