访问JSON对象内的值

时间:2014-01-01 06:47:10

标签: java android json eclipse

这是一些JSON输出:

{
    "model": "model/xxx",
    "source_status": true,
    "objective_field": "000007",
    "locale": "en-US",
    "credits": 0.01,
    "query_string": "",
    "private": true,
    "subscription": true,
    "updated": "2014-01-01T05:15:47.560445",
    "created": "2014-01-01T05:15:47.560417",
    "number_of_models": 1,
    "description": "",
    "name": "Prediction for Category",
    "prediction_path": {
        "path": [],
        "next_predicates": [
            {
                "value": 1.12945,
                "field": "00000e",
                "count": 6483,
                "operator": ">"
            },
            {
                "value": 1.12945,
                "field": "00000e",
                "count": 3517,
                "operator": "<="
            }
        ],
        "bad_fields": [],
        "confidence": 0.32562,
        "unknown_fields": [],
        "objective_summary": {
            "categories": [
                [
                    "Whale",
                    1626
                ],
                [
                    "Minnow",
                    1953
                ],
                [
                    "Fish",
                    3073
                ],
                [
                    "Free",
                    3348
                ]
            ]
        }
    },
    "fields": {
        "00000e": {
            "column_number": 14,
            "optype": "numeric",
            "datatype": "double",
            "order": 5,
            "preferred": true,
            "prefix": "$",
            "name": "Monthly Spend"
        },
        "000007": {
            "optype": "categorical",
            "order": 3,
            "description": "",
            "name": "Category",
            "term_analysis": {
                "enabled": true
            },
            "label": "",
            "column_number": 7,
            "datatype": "string",
            "preferred": true
        }
    },
    "tags": [],
    "shared": false,
    "objective_field_name": "Category",
    "status": {
        "progress": 1,
        "message": "The prediction has been created",
        "elapsed": 0.028,
        "code": 5
    },
    "resource": "prediction/52c3a483e61ab00496000000",
    "objective_fields": [
        "000007"
    ],
    "model_status": true,
    "prediction": {
        "000007": "Free"
    },
    "input_data": {},
    "dataset_status": true,
    "tlp": 1,
    "confidence": 0.3245,
    "code": 201,
    "missing_strategy": 0,
    "category": 0,
    "source": "source/xxxxx",
    "dataset": "dataset/xxxxx",
    "model_type": 0,
    "output": "Free"
}

如何在Confidence内获取prediction_path值?

2 个答案:

答案 0 :(得分:2)

解析json如下

JSONObject jb = new JSONObject("my string");
JSONObject jb1= jb.getJSONObject("prediction_path");
String confidence = jb1.getDouble("confidence");

你的JSON

{   // JSONObject node
 "model": "model/xxx",

 prediction_path": { // json object prediction path
 "confidence": 0.32562,  

http://developer.android.com/reference/org/json/JSONObject.html

答案 1 :(得分:1)

String line = "";
String NL = System.getProperty("line.separator");

in = new BufferedReader(new InputStreamReader(YourHttpResponse
                    .getEntity().getContent()));

while ((line = in.readLine()) != null) {
        sb.append(line + NL);
 }

in.close();
json = sb.toString();

然后:

JSONObject _jasonObj = new JSONObject(json);
JSONObject _json = new JSONObject((_jasonObj.getString("prediction_path"));
String _confidence = _json.get("confidence");
相关问题