我在表单中有一个JSON对象
{ “PAGEID”:1, “stgId”:1, “用户id”:2 “requestStageId”:0 “requestPageId”:0, “回答”:[{ “QID”: “6”,“ansValue “:” 1 “},{” QID “:” 11" , “ansValue”: “10”}]}
我收到的数据为:
long uid = (long)inputJsonObj.get("userId");
long stgid = (long)inputJsonObj.get("stgId");
long pgid = (long)inputJsonObj.get("pageId");
JSONArray answer_Array = (JSONArray) inputJsonObj.get("answer");
即,answer_Array conatins:
[{ “QID”: “6”, “ansValue”: “1”},{ “QID”: “11”, “ansValue”: “10”}]
现在我需要转换JSONArray answer_Array 到LinkedHashMap,其中 qid 将是键和 ansValue 将是值。
怎么办呢?
答案 0 :(得分:1)
您可以迭代数组并使用此方法
public static Map parseStringToMap(String param)
{
Map result = new HashMap<String,String>();
if(param!=null)
{
StringBuffer sb = new StringBuffer(param);
try
{
String str = sb.substring(1, sb.length()-1);
String[] srArray = str.split(",");
String tempStr = null;
for(int i=0;srArray!=null && i<srArray.length;i++)
{
tempStr = srArray[i];
if(tempStr!=null && tempStr.contains(":"))
{
String[] keyVal = tempStr.split(":");
if(keyVal.length>1)
result.put(keyVal[0], keyVal[1]);
else
result.put(keyVal[0], "");
}
}
}
catch(Exception e)
{
System.out.println("In JSONConverter: Issue while parsing the JSON => "+e.getMessage());
e.printStackTrace();
}
}
return result;
}
答案 1 :(得分:1)
您可以编写一个辅助方法,将答案数组作为输入并返回问答地图。
private static Map<String, String> getHashMapFromJson(JSONArray answer_Array) {
Map<String, String> qaMap = new LinkedHashMap<String, String>();
Iterator<JSONObject> iterator = answer_Array.iterator();
while (iterator.hasNext()) {
JSONObject value = iterator.next();
qaMap.put((String) value.get("qId"), (String) value.get("ansValue"));
}
return qaMap;
}
答案 2 :(得分:1)
您可以执行(避免覆盖):
之类的操作Map result = new HashMap<String,String>();
for( int i = 0 ; i < jArray.length() ; i++ ){
JSONObject object = jArray.getJSONObject(i);
result.put(object.getString("qId"), object.getString("ansValue"));
}
for (Object variableName: result.keySet()){
System.out.println(String.valueOf(variableName));
System.out.println(result.get(variableName));
}
我完成"qId"+i
的主要原因是你想要Hashmap中的数据,所以如果它是相同的密钥,那么它就是overwrite the already exist
。并且您希望qId
为关键。
最好使用List
代替它,并制作一类完美的数据。
或者您可以尝试
我的代码(带列表):
static class KeyVal{
String qkey; String qval; String akey; String aval;
public String getQkey() {return qkey;}
public void setQkey(String qkey) { this.qkey = qkey;}
public String getQval() {return qval;}
public void setQval(String qval) {this.qval = qval;}
public String getAkey() {return akey;}
public void setAkey(String akey) {this.akey = akey;}
public String getAval() {return aval;}
public void setAval(String aval) {this.aval = aval;}
}
List<KeyVal> data = new ArrayList<>();
KeyVal key;
for( int i = 0 ; i < ja.length() ; i++ ){
JSONObject object = ja.getJSONObject(i);
key = new KeyVal();
key.setQkey("qId"); key.setQval(object.getString("qId"));
key.setAkey("ansValue"); key.setAval(object.getString("ansValue"));
data.add(key);
}
for( int i = 0 ; i < data.size() ; i++ ){
System.out.println(data.get(i).getQkey() + " : " + data.get(i).getQval());
System.out.println(data.get(i).getAkey() + " : " + data.get(i).getAval());
}
答案 3 :(得分:0)
此方法适用于我 - &gt;
/* Extract The JSONArray and insert The qid and ansValue into the HashMap<String,String> */
JSONArray answer = (JSONArray) inputJsonObj.get("answer");
JSONParser parser3 = new JSONParser();
JSONObject jsonObject3 = null;
for(int i=0;i< answer.size();i++){
System.out.println(answer.get(i).toString());
Object obj3 = parser3.parse(answer.get(i).toString());
jsonObject3 = (JSONObject) obj3;
String qid = (String) jsonObject3.get("qId");
String ansValue = (String) jsonObject3.get("ansValue");
map.put(qid, ansValue);
}