我见过很多例子,但没有一个像我想要的那样。
考虑我有JSONObject
喜欢:
[ {
"id" : "572add95e4b0b04f4d502a3c",
"amount" : 109.27,
"sourceCurrency" : "MXN",
"targetCurrency" : "USD",
"recipientBankId" : "572add95e4b0b04f4d502a37",
"iban" : "5805742027",
"created" : "2016-05-05T05:43:49.194"
}, {
"id" : "572add95e4b0b04f4d502a3e",
"amount" : 722.41,
"sourceCurrency" : "GBP",
"targetCurrency" : "INR",
"recipientBankId" : "572add95e4b0b04f4d502a32",
"iban" : "4688276585",
"created" : "2016-05-05T05:43:49.2"
}]
我希望访问第二个json和iban
值。
我该怎么做?
答案 0 :(得分:1)
使用您的json内容
String json = "[ {\n" +
" \"id\" : \"572add95e4b0b04f4d502a3c\",\n" +
" \"amount\" : 109.27,\n" +
" \"sourceCurrency\" : \"MXN\",\n" +
" \"targetCurrency\" : \"USD\",\n" +
" \"recipientBankId\" : \"572add95e4b0b04f4d502a37\",\n" +
" \"iban\" : \"5805742027\",\n" +
" \"created\" : \"2016-05-05T05:43:49.194\"\n" +
"}, {\n" +
" \"id\" : \"572add95e4b0b04f4d502a3e\",\n" +
" \"amount\" : 722.41,\n" +
" \"sourceCurrency\" : \"GBP\",\n" +
" \"targetCurrency\" : \"INR\",\n" +
" \"recipientBankId\" : \"572add95e4b0b04f4d502a32\",\n" +
" \"iban\" : \"4688276585\",\n" +
" \"created\" : \"2016-05-05T05:43:49.2\"\n" +
"}]";
首先需要从json内容中获取JSONArray:
JSONArray array = new JSONArray(json);
然后你读取数组中的第二个(在索引1处)JSONObject:
JSONObject o = array.getJSONObject(1);
最后你从JSONObject中读到了iban:
String secondIban = o.getString("iban");
System.out.println(secondIban);
当然,所有这些都用try / catch包围来捕捉JSONException
:
try {
JSONArray array = new JSONArray(json);
JSONObject o = array.getJSONObject(1);
String secondIban = o.getString("iban");
System.out.println(secondIban);
}catch(JSONException jse){
jse.printStackTrace();
}
注意强>
如果您想知道iban字段不存在,请使用o.getString("iban")
。
如果字段丢失,将抛出JSONException。
如果您可以使用空字符串""
作为最终缺失字段的默认值,请使用o.optString("iban")
来读取该字段。
答案 1 :(得分:0)
你可以做类似下面的事情。
right_mc.visible=false;
wrong_mc.visible=false;
var orig1X:Number=item1_mc.x;
var orig1Y:Number=item1_mc.y;
var orig2X:Number=item2_mc.x;
var orig2Y:Number=item2_mc.y;
var orig3X:Number=item3_mc.x;
var orig3Y:Number=item3_mc.y;
item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);
item2_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item2_mc.addEventListener(MouseEvent.MOUSE_UP, item2Release);
item3_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item3_mc.addEventListener(MouseEvent.MOUSE_UP, item3Release);
done_btn.addEventListener(MouseEvent.CLICK, checkAnswers);
reset_btn.addEventListener(MouseEvent.CLICK, reset);
item1_mc.buttonMode=true;
item2_mc.buttonMode=true;
item3_mc.buttonMode=true;
function dragTheObject(event:MouseEvent):void
{
var item:MovieClip=MovieClip(event.target);
item.startDrag();
var topPos:uint=this.numChildren-5;
this.setChildIndex(item, topPos);
}
function item1Release(event:MouseEvent):void
{
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone1_mc.hitTestPoint(item.x,item.y))
{
item.x=dropZone1_mc.x;
item.y=dropZone1_mc.y;
}
else
{
item.x=orig1X;
item.y=orig1Y;
}
}
function item2Release(event:MouseEvent):void
{
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone2_mc.hitTestPoint(item.x,item.y))
{
item.x=dropZone2_mc.x;
item.y=dropZone2_mc.y;
}
else
{
item.x=orig2X;
item.y=orig2Y;
}
}
function item3Release(event:MouseEvent):void
{
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone3_mc.hitTestPoint(item.x,item.y))
{
item.x=dropZone3_mc.x;
item.y=dropZone3_mc.y;
}
else
{
item.x=orig3X;
item.y=orig3Y;
}
}
function checkAnswers(event:MouseEvent):void
{
if (dropZone1_mc.hitTestPoint(item1_mc.x,item1_mc.y) &&
dropZone2_mc.hitTestPoint(item2_mc.x,item2_mc.y) &&
dropZone3_mc.hitTestPoint(item3_mc.x,item3_mc.y))
{
wrong_mc.visible = false;
right_mc.visible = true;
}
else
{
wrong_mc.visible = true;
right_mc.visible = false;
}
}
function reset(event:MouseEvent):void {
item1_mc.x=orig1X;
item1_mc.y=orig1Y;
item2_mc.x=orig2X;
item2_mc.y=orig2Y;
item3_mc.x=orig3X;
item3_mc.y=orig3Y;
right_mc.visible=false;
wrong_mc.visible=false;
}
function itemRelease(event:MouseEvent):void {
var thisItem:MovieClip = MovieClip(event.target);
thisItem.stopDrag();
if (dropZone1_mc.hitTestPoint(thisItem.x,thisItem.y))
{
thisItem.x = dropZone1_mc.x;
thisItem.y = dropZone1_mc.y;
}
else if (dropZone2_mc.hitTestPoint(thisItem.x,thisItem.y))
{
thisItem.x = dropZone2_mc.x;
thisItem.y = dropZone2_mc.y;
}
else if (dropZone3_mc.hitTestPoint(thisItem.x,thisItem.y))
{
thisItem.x = dropZone3_mc.x;
thisItem.y = dropZone3_mc.y;
}
else if (thisItem==item1_mc)
{
event.target.x = orig1X;
event.target.y = orig1Y;
}
else if (thisItem==item2_mc)
{
event.target.x = orig2X;
event.target.y = orig2Y;
}
else {
event.target.x = orig3X;
event.target.y = orig3Y;
}
}
答案 2 :(得分:0)
使用optString
代替getString
。
optString
- 如果在JSON中找不到元素/键,则返回空字符串。
getString
- 会抛出异常。
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
================================
public static void main(String[] args) throws JSONException {
String str = "[{\"id\":\"572add95e4b0b04f4d502a3c\",\"amount\":109.27,\"sourceCurrency\":\"MXN\",\"targetCurrency\":\"USD\",\"recipientBankId\":\"572add95e4b0b04f4d502a37\",\"iban\":\"5805742027\",\"created\":\"2016-05-05T05:43:49.194\"},{\"id\":\"572add95e4b0b04f4d502a3e\",\"amount\":722.41,\"sourceCurrency\":\"GBP\",\"targetCurrency\":\"INR\",\"recipientBankId\":\"572add95e4b0b04f4d502a32\",\"iban\":\"4688276585\",\"created\":\"2016-05-05T05:43:49.2\"}]";
JSONArray jsonArray = new JSONArray(str);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);
/* optString - will return blank string if element not found */
String iban = jsonObject.optString("iban");
System.out.println(iban);
}
}
答案 3 :(得分:0)
你应该做的是将字符串转换为json:
public JsonObject parseJsonString(String jsonString) {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonString);
return jsonObject;
}
然后提取您所追求的值:
final String iban = jsonObject.get("iban").getAsString();
答案 4 :(得分:0)
要从JSON读取值,您只能使用下一个方法:
使用哪种方法取决于您,因为所有方法都有优势和劣势。
发动机:
我想你只想从json中选择一个部分,所以xpath就可以选择语法如$[1].iban
(JsonPath)