我在android中使用PubNub
API来获取实时数据,但我需要一些特定的值而不是整数。如何获得示例c7 & c9
的值。我希望他们绘制一个实时图表。
@Override
public void message(PubNub pubnub, PNMessageResult message) {
System.out.println(message);
}
我得到的结果: -
PNMessageResult(message = {“c1”:27,“c2”:3,“c3”:2017,“c4”:15,“c5”:56,“c6”:52,“c7”:15.07, “C8”: “p&安培; Q”, “C9”:24.86, “C10”:82, “C11”:32.74}`
答案 0 :(得分:0)
Make json object and parse specific value:
{
"c1":27,
"c2":3,
"c3":2017,
"c4":15,
"c5":56,
"c6":52,
"c7":15.07,
"c8":"P&Q",
"c9":24.86,
"c10":82,
"c11":32.74
}
try{
JSONObject jsonObject = new JSONObject("your string");
double c7 = jsonObject.getDouble("c7");
double c9 = jsonObject.getDouble("c9");
}
catch(JSONException e){
e.printStackTrace()
}
答案 1 :(得分:0)
如果你解析json api中的任何值,你应该为那个
创建json对象例如:
{
"c1":27,
"c2":3,
"c3":2017,
"c4":15,
"c5":56,
"c6":52,
"c7":15.07,
"c8":"P&Q",
"c9":24.86,
"c10":82,
"c11":32.74
}
try{
JSONObject jsonObject = new JSONObject("string");
String c8 = jsonObject.getString("c8");
}
catch(Exception e){
}
答案 2 :(得分:0)
{"c1":27,
"c2":3,
"c3":2017,
"c4":15,
"c5":56,
"c6":52,
"c7":15.07,
"c8":"P&Q",
"c9":24.86,
"c10":82,
"c11":32.74
}
//In place of your string place a response string which /////you get after successful API call
try{
JSONObject jsonObject = new JSONObject("your string");
double c7 = jsonObject.getDouble("c7");
double c9 = jsonObject.getDouble("c9");
}
catch(JSONException e){
e.printStackTrace();//shouldn't be empty
}
您需要下载org.json库并将其包含在您的程序中。
答案 3 :(得分:0)
String s="{
"c1":27,
"c2":3,
"c3":2017,
"c4":15,
"c5":56,
"c6":52,
"c7":15.07,
"c8":"P&Q",
"c9":24.86,
"c10":82,
"c11":32.74`
}";
try{
JSONObject jsonObject = new JSONObject(s);
double c7 = jsonObject.getDouble("c6");
double c9 = jsonObject.getDouble("c2");
}
catch(JSONException e){
e.printStackTrace()
}
答案 4 :(得分:0)
您的JSON
邮件包含int
,double
和string
个值。您需要使用Iterator
来获取所有KEY
(c1,c2 ... c11),还需要检查其类型(int, double, string
)。最后根据您的需要使用Key(c7, c9..
)值。
这是一个有效的代码。试试这个:
public void parseJson() {
// Your PNMessageResult JSON string
// {"c1":27,"c2":3,"c3":2017,"c4":15,"c5":56,"c6":52,"c7":15.07,"c8":"P&Q","c9":24.86,"c10":82,"c11":32.74}
String jsonStr = "{\"c1\":27,\"c2\":3,\"c3\":2017,\"c4\":15,\"c5\":56,\"c6\":52,\"c7\":15.07,\"c8\":\"P&Q\",\"c9\":24.86,\"c10\":82,\"c11\":32.74}";
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Keys: c1, c2, c3.....c11
Iterator<String> keys = jsonObj.keys();
while(keys.hasNext()) {
String key = keys.next();
Object object = jsonObj.get(key);
if (object instanceof Integer || object instanceof Long) {
long valueLong = ((Number)object).longValue();
Log.d("SUCCESS", "Int value: " + valueLong + "\n" + key + ": " + valueLong);
// Do something with value
} else if (object instanceof Float || object instanceof Double) {
double valueDouble = ((Number)object).doubleValue();
Log.d("SUCCESS", "Double value: " + valueDouble + "\n" + key + ": " + valueDouble);
// Do something with value
} else if (JSONObject.NULL.equals(object)) {
Object valueNull = null;
// Do something with value
} else {
String valueString = jsonObj.getString(key);
Log.d("SUCCESS", "String value: " + valueString + "\n" + key + ": " + valueString);
// Do something with value
}
}
} catch (final JSONException e) {
Log.e("FAILED", "Json parsing error: " + e.getMessage());
}
}
}
以下是输出:
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/MESSAGE: PNMessage: {"c1":27,"c2":3,"c3":2017,"c4":15,"c5":56,"c6":52,"c7":15.07,"c8":"P&Q","c9":24.86,"c10":82,"c11":32.74}
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Int value: 27
c1: 27
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Int value: 3
c2: 3
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Int value: 2017
c3: 2017
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Int value: 15
c4: 15
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Int value: 56
c5: 56
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Int value: 52
c6: 52
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Double value: 15.07
c7: 15.07
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: String value: P&Q
c8: P&Q
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Double value: 24.86
c9: 24.86
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Int value: 82
c10: 82
03-29 12:15:26.848 12536-12536/com.ferdous.collapsibletoolbar D/SUCCESS: Double value: 32.74
c11: 32.74
希望这会有所帮助〜