How to parse JSON string array into Java arraylist
I have this json:
{
"a": [
"A-lore ipsum 1",
"A-lore ipsum 2",
"A-lore ipsum 3"
],
"b": [
"B-lore ipsum 1",
"B-lore ipsum 2",
"B-lore ipsum 3"
]
}
this is my original code :
//v refers to **a or b**
public ArrayList getJSON(String v){
ArrayList<String> list = new ArrayList<String>();
InputStream is = getResources().openRawResource(R.raw.myjson);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
Reader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
String json = writer.toString();
JSONObject jsonObject = new JSONObject(json);
list = jsonObject.getString(v); //how to create list according to category
} catch (Exception e) { e.printStackTrace();}
return list;
}
So I would like my list to look like:
if v = 'a';
list = {"A-lore ipsum 1","A-lore ipsum 2","A-lore ipsum 3"};
if v = 'b';
list = {"B-lore ipsum 1","B-lore ipsum 2","B-lore ipsum 3"};
-
What were the results when you ran this code? You can edit your question to add the results.Theresa– Theresa2015年02月15日 14:47:51 +00:00Commented Feb 15, 2015 at 14:47
-
I edited my post, take a look now.tziuka– tziuka2015年02月15日 15:04:23 +00:00Commented Feb 15, 2015 at 15:04
-
Having seen your update I fell my answer still meets your requirements, just change the method signature of getJSON to return SomeClass. Also jackson allows you to process the reader directly by calling mapper.readValue(reader, SomeClass.class); the caller knows which list they want, so can call getA/B on the object they are returnedberesfordt– beresfordt2015年02月15日 15:10:17 +00:00Commented Feb 15, 2015 at 15:10
-
It seams complicated to me, with another class. I would like to use org.json. and some simple method that return a list of stringstziuka– tziuka2015年02月15日 15:15:29 +00:00Commented Feb 15, 2015 at 15:15
-
I've added an answer which returns a List<String>, and uses org.json. I would strongly reccommend trying out jackson or gson howeverberesfordt– beresfordt2015年02月15日 16:51:28 +00:00Commented Feb 15, 2015 at 16:51
3 Answers 3
public class MainActivity extends ActionBarActivity {
private String TAG = MainActivity.class.getSimpleName();
private TextView textView;
ArrayList<String> arrayListA = new ArrayList<String>();
ArrayList<String> arrayListB = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
try {
InputStream is = getAssets().open("myfile.json");
int size = is.available();
byte [] byteArray = new byte[size];
is.read(byteArray);
is.close();
String json = new String(byteArray, "UTF-8");
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray arrayA = jsonObject.getJSONArray("a");
JSONArray arrayB = jsonObject.getJSONArray("b");
for (int i = 0 ; i < arrayA.length(); i++) {
String str = (String) arrayA.get(i);
arrayListA.add(str);
}
for (int i = 0 ; i < arrayB.length(); i++) {
String str = (String) arrayB.get(i);
arrayListB.add(str);
}
for (String str : arrayListA)
Log.d(TAG, "strA : "+str);
for (String str : arrayListB)
Log.d(TAG, "strB : "+str);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Comments
Op has updated the question specifying that org.json is to be used, and either array or list needs to be returned.
I would strongly recommend using a more fully featured json library such as jackson - see Android, JSON array into ArrayList<String> for a jackson way of doing this
The below will achieve what you want with json simple, however as you can see there is some fragility which using the org.json library introduces:
final JSONObject jsonObject = new JSONObject(json);
final JSONArray a = (JSONArray) jsonObject.get("a");
final List<String> returnArray = new ArrayList<>();
for (int i = 0; i < a.length(); i++) {
returnArray.add((String)a.get(i));
}
return returnArray;
Comments
You have a syntax error in the json you have provided above; there should be a comma between a and b:
{
"a": [
"A-lore ipsum 1",
"A-lore ipsum 2",
"A-lore ipsum 3"
],
"b": [
"B-lore ipsum 1",
"B-lore ipsum 2",
"B-lore ipsum 3"
]
}
For parsing json I'd recommend using Jackson (or Gson) rather than the org.json library; I find them nicer to interact with.
The jackson wiki is at http://wiki.fasterxml.com/JacksonHome and has lots of tutorials etc
using jackson it would be:
ObjectMapper mapper = new ObjectMapper();
final SomeClass someClass = mapper.readValue(json, SomeClass.class);
List<String> a = someClass.getA();
where SomeClass is
class SomeClass {
@JsonProperty
private List<String> a;
@JsonProperty
private List<String> b;
public SomeClass() {
}
public List<String> getA() {
return a;
}
public List<String> getB() {
return b;
}
}