Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

ios - Having difficulty parsing a response from an API call

I'm trying to parse the response object from an API call, but having some difficulty.

First, I parsed the returned JSON with following:

let responseObject = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
if let result = responseObject["result"] {
  print(result)
}

Then, when I log the result, following is what I get:

{
    "_links" =     {
        next = "/api/3/action/datastore_search?limit=50&id=22f223e7-73f7-4842-935c-80a0ba5c3e5b&offset=50";
        start = "/api/3/action/datastore_search?limit=50&id=22f223e7-73f7-4842-935c-80a0ba5c3e5b";
    };
    fields =     (
                {
            id = "_id";
            type = int;
        },
                {
            id = package;
            info =             {
                label = "";
                notes = "Unique, normalized, name of dataset.
                "type_override" = "";
            };
            type = text;
        }
    )
}

I tried parsing it again:

let finalResult = (try? JSONSerialization.jsonObject(with: result)) as? [String: Any]

But, I get the following error:

No exact matches in call to class method 'jsonObject'

Update

if let result = responseObject["result"] as? [String: Any] {
    if let finalResult = result["records"] {
        print(finalResult)
    }
}

When I log this, I get the following:

(
        {
        "_id" = 186;
        accessibility = 1;
        completeness = "0.6899999999999999";
        freshness = "0.5";
        grade = Silver;
        "grade_norm" = Silver;
        metadata = "0.84";
        package = "air-conditioned-and-cool-spaces-heat-relief-network";
        "recorded_at" = "2019-12-17T20:24:09";
        score = "0.78";
        "score_norm" = "0.76";
        usability = "0.86";
        version = "v0.1.0";
    },
        {
        "_id" = 187;
        accessibility = 1;
        completeness = 1;
        freshness = 0;
        grade = Bronze;
        "grade_norm" = Bronze;
        metadata = "0.25";
        package = "air-conditioned-public-places-cooling-centres";
        "recorded_at" = "2019-12-17T20:24:09";
        score = "0.54";
        "score_norm" = "0.31";
        usability = "0.85";
        version = "v0.1.0";
    },
)

When I tried to iterate this:

for (key, value) in finalResult {
    print("key", key)
    print("value", value)
}

I get the following error:

Tuple pattern cannot match values of none-tuple type


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...