[英]How to extract data from a json object in android
In my application I am receiving a JSON object as 在我的应用程序中,我收到一个JSON对象,
{"secQueList":{"1":"Which is your favorite book?","2":"Who was your childhood hero?","3":"What is your pet's name?","4":"What make was your first car or bike?","5":"What is your favorite color?","6":"Which is your favorite sports team?","7":"What was the name of your school?","8":"What is your mother's maiden name?","9":"Which is your birthplace?","10":"Which is your favourite sport?","11":"Which is your favourite place of visit?"},"que1":null,"ans1":null,"message":null,"fieldErrors":null}
{“ secQueList”:{“ 1”:“您最喜欢哪本书?”,“ 2”:“您的童年英雄是谁?”,“ 3”:“您的宠物叫什么名字?”,“ 4”:“您的第一辆汽车或自行车是什么牌子的?“,” 5“:”您最喜欢的颜色是什么?“,” 6“:”您最喜欢的运动队是哪个?“,” 7“:”您的学校叫什么名字?“,” 8“:”您母亲的娘家姓是什么?“,” 9“:”您的出生地是哪个?“,” 10“:”您最喜欢的运动是哪个?“,” 11“:”您的哪个是?最喜欢的参观地点?“},” que1“:null,” ans1“:null,” message“:null,” fieldErrors“:null}
I am not able to figure out how exactly should i parse this object. 我无法弄清楚我应该如何解析该对象。 I tried using the below code but as this not a JSONArray it throws an exception.
我尝试使用下面的代码,但由于这不是JSONArray,因此会引发异常。
String getParam(String code, String element){
try {
String base = this.getItembyID(code);
JSONObject product = new JSONObject(base);
JSONArray jarray = product.getJSONArray("item");
String param = jarray.getJSONObject(0).getString("name");
return param;
} catch (JSONException e) {
e.printStackTrace();
return "error";
}
}
String base = this.getItembyID(code);
JSONObject product = new JSONObject(base);
JSONOBject secQueListJson = product.getJSONObject("secQueList");
// Get all json keys "1", "2", "3" etc in secQueList, so that we can get values against each key.
Set<Map.Entry<String, JsonElement>> entrySet = secQueListJson .entrySet ();
Iterator iterator = entrySet.iterator ();
for (int j = 0; j < entrySet.size (); j++) {
String key = null; //key = "1", "2", "3" etc
try {
Map.Entry entry = (Map.Entry) iterator.next ();
key = entry.getKey ().toString ();
//key = "1", "2", "3" etc
}
catch (NoSuchElementException e) {
e.printStackTrace ();
}
if (!TextUtils.isEmpty (key)) {
Log.d ("JSON_KEY", key);
String value = secQueListJson.getString(key);
//for key = "1", value = "Which is your favorite book?"
//for key = "2", value = "Who was your childhood hero?"
}
}
I recommend you use sites for json formater which show to you the types of json elements as http://json.parser.online.fr/ and you can use Gson library for parsing json by using pojo class 我建议您使用json格式化程序的网站,该网站向您显示json元素的类型,如http://json.parser.online.fr/,并且可以使用poson类使用Gson库解析json。
public class secQueList {
public String que1;
public int ans1;
public String message;
public String nextPage;
public QuestionList secQueList;
}
public class QuestionList {
@SerializedName("1")
public String ques1;
@SerializedName("2")
public int ques2;
@SerializedName("3")
public String ques3;
@SerializedName("4")
public String ques4;
@SerializedName("5")
public String ques5;
@SerializedName("6")
public String ques6;
@SerializedName("7")
public int ques7;
@SerializedName("8")
public String ques8;
@SerializedName("9")
public String ques9;
@SerializedName("10")
public String ques10;
@SerializedName("11")
public String ques11;
}
or you can use parse using built in JSON Object 或者您可以使用内置的JSON对象使用解析
String jsonBody = the string you want to parse
JSONObject quesJsonBody = new JSONObject(jsonBody);
JSONOBject quesJson = quesJsonBody.getJSONObject("secQueList");
String quesJson1 = quesJson.getString("1");
String quesJson2 = quesJson.getString("2");
String quesJson3 = quesJson.getString("3");
String quesJson4 = quesJson.getString("4");
String quesJson5 = quesJson.getString("5");
String quesJson6 = quesJson.getString("6");
String quesJson7 = quesJson.getString("7");
String quesJson8 = quesJson.getString("8");
String quesJson9 = quesJson.getString("9");
String quesJson10 = quesJson.getString("10");
String quesJson11 = quesJson.getString("11");
String que1 = quesJsonBody.getString("que1");
String ans1 = quesJsonBody.getString("ans1");
String message = quesJsonBody.getString("message");
String fieldErrors = quesJsonBody.getString("fieldErrors");
Try something like this: 尝试这样的事情:
JSONObject jsonObject = new JSONObject(code);
JSONObject jsonObject1 = jsonObject.getJSONObject("secQueList");
for (int i=0;i<jsonObject1.length();i++){
String msg = jsonObject1.getString(String.valueOf(i));
}
You can try to use Gson to parse it. 您可以尝试使用Gson进行解析。 I used code below in my App in similiar case:
我在类似的情况下在我的应用中使用了以下代码:
ArrayList<String> myArrayList;
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<String>>(){}.getType();
myArrayList= gson.fromJson(code, type);
You have to add gson to your build.grandle to have it working 您必须将gson添加到build.grandle才能使其正常工作
compile 'com.google.code.gson:gson:2.2.+'
Extract Data from JSON String 从JSON字符串中提取数据
public void ReadSMS_JSON(String JSON_String) {
String smsID = "";
String phone = "";
String message = "";
JSONObject jsonResponse = new JSONObject(JSON_String);
JSONArray jsonMainNode = jsonResponse.optJSONArray("tblsms");
if(jsonMainNode.length()>0){
JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);
smsID = jsonChildNode.optString("sms_id");
phone = jsonChildNode.optString("sms_number");
message = jsonChildNode.optString("sms_message");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.