I have a JSON object
"methodSet":[{"num":1,"methodName":1,"methodStatus":1},
{"num":2,"methodName":2,"methodStatus":1}]
I need to put the methodName and methodStatus into MethodClass array.
Based from the below code, I always get the last value for my MethodClass [2][1]. The expected result that I want is [1][1],[2][1]. Can I have any idea how I can accumulate the value for my methodSet? What are the method that I can use to Append my 2D array?
JSONArray arr=(JSONArray)obj.get("methodSet");
int lengtharr = arr.length();
if (arr != null) {
JSONObject objMethod1;
String MethodName, MethodStatus;
for (Object o1 : arr) {
objMethod1 = (JSONObject) o1;
MethodName = String.valueOf(objMethod1.get("methodName"));
MethodStatus = String.valueOf(objMethod1.get("methodStatus"));
int resultMethodName = Integer.parseInt(MethodName);
int resultMethodStatus = Integer.parseInt(MethodStatus);
for (int j = 0; j < lengtharr; j++) {
methodSetFinal[i][j] = new MethodClass();
methodSetFinal[i][j].setmethodName(resultMethodName);
methodSetFinal[i][j].setmethodStatus(resultMethodStatus);
methodSet [i][j] = methodSetFinal [i][j];
}
}
}
MethodClass Code:
public class MethodClass {
private int methodName;
private int methodStatus;
public MethodClass() {
methodName = 0;
methodStatus = 0;
}
public int getmethodName() {
return methodName;
}
public int getmethodStatus() {
return methodStatus;
}
public void setmethodName(int i) {
this.methodName = i;
}
public void setmethodStatus(int status) {
this.methodStatus = status;
}
}
iisn't defined here. And why do you have a 2D array anyway? Seems like you should have a 1D array (orList), where each element is aMethodClassinstance. Not sure why you have twoMethodClasses for eachJSONObject. They are just duplicates of each other.MethodClassholds two pieces of data (methodNameandmethodStatus). So having an array ofMethodClassinstances will give you the[1][1], [2][1]thing you're looking for. You would haveMethodClass,MethodClasswhere the first holdsmethodName = 1,methodStatus = 1and the second which holdsmethodName = 2,methodStatus = 1.