在我的应用程序中,我收到一个JSON对象
{" secQueList":{" 1":"您最喜欢哪本书?"," 2":&#34 ;谁是你儿时的英雄?"," 3":"你宠物的名字是什么?"," 4":& #34;你的第一辆车或自行车是什么产品?"," 5":"你最喜欢的颜色是什么?"," 6": "哪个是你最喜欢的运动队?"," 7":"你学校的名字是什么?"," 8" :"你母亲的婚前姓名是什么?"," 9":"哪个是你的出生地?"," 10&# 34;:"您最喜欢的运动是什么?",#34; 11":"您最喜欢哪个访问地点?"}," que1& #34;:空," ANS1":空,"消息":空," fieldErrors":空}
我无法弄清楚究竟应该如何解析这个对象。 我尝试使用下面的代码,但因为这不是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";
}
}
答案 0 :(得分:2)
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?"
}
}
答案 1 :(得分:2)
我建议您使用json formater的网站,向您展示json元素的类型为http://json.parser.online.fr/ 和 您可以使用Gson库通过使用pojo类
来解析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;
}
或者您可以使用内置的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");
答案 2 :(得分:1)
尝试这样的事情:
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));
}
答案 3 :(得分:1)
你可以尝试使用Gson来解析它。我在我的应用程序中使用了类似的代码:
ArrayList<String> myArrayList;
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<String>>(){}.getType();
myArrayList= gson.fromJson(code, type);
您必须将gson添加到build.grandle才能使其正常工作
compile 'com.google.code.gson:gson:2.2.+'
答案 4 :(得分:0)
从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");
}
}