-1

My JSON string is :[["Canada", 3], ["SriLanka", 6], ["China", 5], ["UK", 4]]

alexbt
  • 16,415
  • 6
  • 78
  • 87

4 Answers4

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();
}
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
0

This is not standard for JSON. You have to build it this way:

[{"country":"Canada", "x":3}, {"country":"SriLanka", "x":6}, {"country":"China", "x":5}...]

Brackets mean JSONArray

Anyway, if want to split a String.. use Java Basics... It's not an Android issue.

Split by regex and patterns, using split String basic method, or dealing them as a JSONArray..

You can find a lot of information in other posts.

PS: Too long to be a comment.

I hope I would help you.

Juan Aguilar Guisado
  • 1,687
  • 1
  • 12
  • 21
0

You can do it like this:

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("");
    }
Aakash
  • 5,181
  • 5
  • 20
  • 37
0

A correct JSON format should be as follows,

[{'name':'Canada', 'value':'3'}, {'name':'SriLanka', 'value':'6'}, {'name': 'China', 'value':'5'}]

Then you can do,

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

where

Country is,

public class Country {
   private int value;
   private String name;
   //getters and setters
}
diyoda_
  • 5,274
  • 8
  • 57
  • 89