如何在android中打破这个Json字符串?

时间:2015-09-16 22:04:41

标签: android json

我的JSON字符串是:[[“Canada”,3],[“SriLanka”,6],[“China”,5],[“UK”,4]]

4 个答案:

答案 0 :(得分:0)

try {
  JSONArray ja = new JSONArray("[['Canada', 3], ['SriLanka', 6], ['China', 5], ['UK', 4]]");
  for(int i=0;i<ja.length();i++){
    JSONArray ja1 = ja.getJSONArray(i);
    System.out.println("Country Name = "+ja1.get(0));
  }
} catch (JSONException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

这不是JSON的标准。你必须以这种方式构建它:

with x as (
    select 1 col1, 1 col2, 2 col3 from dual union all
    select 1, 3, 33 from dual union all
    select 2, 4, 5 from dual union all
    select 3, 6, 40 from dual union all
    select 1, 2, 5 from dual union all
    select 3, 5, 10 from dual
 )
 select col1, col2, col3,
        sum( case when col3 > 30 then 1 else 0 end ) 
          over( partition by col1
                order by col2 ) + 1 rnk
   from x

括号是指JSONArray

无论如何,如果想分割一个String ..使用Java Basics ......这不是一个Android问题。

使用正则表达式和模式进行拆分,使用split String基本方法,或将它们作为JSONArray处理..

您可以在其他帖子中找到很多信息。

PS:评论太久了。

我希望我会帮助你。

答案 2 :(得分:0)

你可以这样做:

JSONArray array=new JSONArray(jsonString);
    for(int i=0;i<array.length();i++){
     JSONArray array1=array.getJSONArray(i);
     for(int j=0;j<array1.length();j++){
      System.out.print(array1.getString(0));
      System.out.print(array1.getInt(1));
      }
      System.out.println("");
    }

答案 3 :(得分:0)

正确的JSON格式应如下所示,

  

[{&#39; name&#39;:&#39; Canada&#39;,&#39; value&#39;:&#39; 3&#39;},{&#39; name&#39; ;:&#39;斯里兰卡&#39;,&#39;价值&#39;:&#39; 6&#39;},   {&#39; name&#39;:&#39; China&#39;,&#39; value&#39;:&#39; 5&#39;}]

然后就可以了,

List<Country> list2 = mapper.readValue(jsonString, 
TypeFactory.defaultInstance().constructCollectionType(List.class,  Country.class));

其中

国家是,

public class Country {
   private int value;
   private String name;
   //getters and setters
}