我正在尝试从下面提到的values (ID as a string, branches of address_one and two)
响应中获取特定JSON
。请帮我。
我的回复
{
"response":{
"school":"inox",
"grad":"first",
},
"ID":"1234567890101112",
"branches":{
"address_one":"ISA",
"address_two":"DOA"
},
"my school":{
"inox":"first"
},
}
我的代码:
try {
JSONObject data = response.getJSONObject("response");
String school_value = data.getString(KEY_SCHOOL);
String grad_value = data.getString(KEY_GRAD);
System.out.println("RESPONSE:" + data + school_value + grad_value);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
ID
位于response
JSONObject中。 address_one
内的address_two
和branches
个JSONObject位于response
JSONObject内部:
// get ID
String strID= response.optString("ID");
// get branches JSONObject
JSONObject jsonBranches = response.getJSONObject("branches");
// get address_one and address_two from jsonBranches
//address_one
String address_one= jsonBranches.optString("address_one");
//address_two
String address_two= jsonBranches.optString("address_two");
答案 1 :(得分:0)
如果你的字符串中的响应然后首先在json对象中转换它,然后在解析之后如果没有得到则在检查你的参数与json key匹配后
JSONObject response =new JSONObject(response_string);
JSONObject data = response.getJSONObject("response");
String school_value = data.getString("school");
String grad_value = data.getString("grad");
答案 2 :(得分:0)
首先,您发布的代码段不是有效的json。我认为,正确的形式是在关闭大括号之前没有逗号:
{
"response": {
"school": "inox",
"grad": "first"
},
"ID": "1234567890101112",
"branches": {
"address_one": "ISA",
"address_two": "DOA"
},
"my school": {
"inox": "first"
}
}
除此之外,您的代码似乎没问题。要获取地址,首先获取分支对象,然后获取地址字符串:
JSONObject branches = response.getJSONObject("branches");
String address1 = branches.getString("address_one");
String address2 = branches.getString("address_two");
希望它能奏效。
答案 3 :(得分:0)
像这样使用。
.previewing{
padding-bottom:10px;
padding-top:0px;
margin-bottom:0px;
border-width: 0px;
border-style:none;
transition: all .2s ease-in-out;
-webkit-animation: fadein 2s;
-moz-animation: fadein 2s;
-ms-animation: fadein 2s;
-o-animation: fadein 2s;
animation: fadein 2s;
}
@keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
@-moz-keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
@-webkit-keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
@-ms-keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
@-o-keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
.previewing:hover{
transform: scale(1.02);
}
答案 4 :(得分:0)
String id = response.getString("1234567890101112")
JSONObject brances = response.getJSONObject("branches");
String address_one = brances.getString("address_one");
String address_two = brances.getString("address_one");
JSONObject myScool = response.getJSONObject("my school");
String inox = brances.getString("inox");
System.out.println("Responce in Json: " + responce.toString);
System.out.println("Responce extracted: " + school_value + " " + grad_value +
" " + id + " " +address_one + " " + address_two + " " + inox);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}`
答案 5 :(得分:0)
将其替换为您的代码,您将在SOP中打印ID
try {
JSONObject data = response.getJSONObject("response");
String school_value = data.getString(KEY_SCHOOL);
String grad_value = data.getString(KEY_GRAD);
String id=response.getString("ID");
System.out.println("RESPONSE:" + data + school_value + grad_value+id);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}