[英]Android, JSON array into ArrayList<String>
How to parse JSON string array into Java arraylist 如何将JSON字符串数组解析为Java arraylist
I have this json: 我有这个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'; 如果v ='a';
list = {"A-lore ipsum 1","A-lore ipsum 2","A-lore ipsum 3"};
if v = 'b'; 如果v ='b';
list = {"B-lore ipsum 1","B-lore ipsum 2","B-lore ipsum 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();
}
}
}
You have a syntax error in the json you have provided above; 您在上面提供的json中有语法错误; there should be a comma between a and b:
a和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; 对于解析json,我建议使用Jackson(或Gson)而不是org.json库。 I find them nicer to interact with.
我发现它们可以更好地进行交互。
The jackson wiki is at http://wiki.fasterxml.com/JacksonHome and has lots of tutorials etc 杰克逊Wiki位于http://wiki.fasterxml.com/JacksonHome ,其中包含许多教程等
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 SomeClass在哪里
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;
}
}
Op has updated the question specifying that org.json is to be used, and either array or list needs to be returned. Op更新了指定使用org.json的问题,并且需要返回数组或列表。
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 我强烈建议您使用功能更全面的json库(例如杰克逊)-参见Android,将JSON数组放入ArrayList <String>中 ,以了解实现此目的的杰克逊方法
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: 下面将通过json简单实现您想要的功能,但是您可以看到使用org.json库引入了一些脆弱性:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.