0

我有java函数,它以json格式返回输出

{"loyalty_score":{"End Value":0.0,"Difference":0.0},"activity_level":{"End Value":0.0,"Difference":0.0}}

我想获取对象的每个值

我尝试下面的代码

JSONObject json = new JSONObject(); 

json = fb.Summary();

System.out.print("\n\n\n Json"+json); 
//{"loyalty_score":{"End Value":0.0,"Difference":0.0},"activity_level":{"End Value":0.0,"Difference":0.0}}

System.out.print( json.get("loyalty_score")); 
//{End Value=0.0, Difference=0.0}

现在如何获得最终价值和差异?

4

2 回答 2

1

尝试使用json.get(attributeName).

JSONObject loyalty = json.getJSONObject("loyalty_score");
double endValue = loyalty.Double("End Value");
System.out.print("End: " + endValue);

看来这json.get("loyalty_score")也是一个 JSONObject。

于 2013-08-26T12:49:52.577 回答
-1

你说json.get("loyalty_score")给你 {End Value=0.0, Difference=0.0},它必须又是一个json对象。您可以像这样将其转换为 String :

<% String jLoyaltyScore = json.get("loyalty_score").toString();%>

现在用作分隔splitjLoyaltyScore,

<%

String splitLoyalty[] = jLoyaltyScore .split("//,");

String splitAgain[] = splitLoyalty[0]. split("="); //SPlitting the first elem again

%>

这将为您提供预期的结果:splitAgain[0], splitAgain[1].

于 2013-08-26T13:07:13.240 回答