[英]How to Get String From Parse JSON without String Id in Android Studio
How do I get a string from Parse JSON with data like below?如何从 Parse JSON 中获取带有如下数据的字符串?
My Data JSON:我的数据 JSON:
[
[
"John Gorman",
"3344764667200003",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Andy William",
"3403032311690003",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Thomas Endry",
"3408932311690078",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Robet Calm",
"3403077711690890",
"Student",
"2018-08-09 08:21:30.807"
]
]
I use the code below, then to get the string what I have to do, I really don't understand, if there is help, I really need to thank you.我使用下面的代码,然后得到字符串我要做什么,我真的不明白,如果有帮助,我真的需要谢谢你。
try {
JSONObject object = new JSONObject(result);
JSONArray jsonArray = object.getJSONArray("");
//JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString("What should I write here");
System.out.println(c.getString(""));
}
I want to get the string from Parse JSON that I have我想从我拥有的 Parse JSON 中获取字符串
How do I get a string for listview, for example:如何获取列表视图的字符串,例如:
John Gorman
3344764667200003
Student
Andy William
3403032311690003
Student
Thomas Endry
3408932311690078
Student
Robet Calm
3403077711690890
Student
A couple of things are wrong with the posted code.发布的代码有一些问题。 See my comments inline:
请参阅我的内联评论:
JSONObject object = new JSONObject(result); ^^^^^^^^^^^^^^^^^^^^^^ the content of result is not an object, but an array. JSONArray jsonArray = object;getJSONArray(""); ^^ a proper JSON document had better not have empty keys; //JSONArray jsonArray = new JSONArray(result). ^^^^^^^^^^^^^^^^^^^^^ this was actually going in the right direction; for (int i = 0. i < jsonArray;length(), i++) { JSONObject c = jsonArray.getJSONObject(i); ^^^^^^^^^^^^^^^^^^^^^^^^^^ the i-th element of the array is not an object, but an array!
Fixing the issues above:修复上述问题:
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray subArray = jsonArray.getJSONArray(i);
for (int j = 0; j < subArray.length(); j++) {
System.out.println(subArray.getString(j));
}
System.out.println();
}
You can do something like this:你可以这样做:
try {
JSONObject object = new JSONObject(result);
JSONArray jsonArray = object.getJSONArray("");
//JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString(0);
String id = c.getString(1);
String student = c.getString(2);
// and so on ... This index refers to the index number on the string inside json object
System.out.println(name);
}
Hope this answers your query.希望这能回答您的问题。
You are receiving a JSONArray not object so use the below code.您收到的是 JSONArray 而不是 object 所以使用下面的代码。
for(int i=0;i<jsonarray.length();i++){
JSONArray array=jsonarray.getJSONArray(i);
for(int j=0;j<array.length();j++){
Log.d(TAG,array.getString(j));
}
}
Following method will construct a list of string from the given Json.以下方法将从给定的 Json 构造一个字符串列表。 You can pass that list in the adapter of the list view.
您可以在列表视图的适配器中传递该列表。
public List<String> getList (String result) throws JSONException {
List<String> list = new LinkedList<>();
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray arr = jsonArray.getJSONArray(i);
StringBuilder sb = new StringBuilder();
for(int j=0;j<arr.length(); j++) {
sb.append(arr.getString(j));
if(j<arr.length()-1) sb.append("\n");
}
list.add(sb.toString());
}
return list;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.