Swift JSON使用网址

时间:2017-08-04 13:50:21

标签: json swift

我正在尝试解析JSON,一切正常,但是当我尝试使用url提供过滤器时,它会在展开可选值时发现nil。我想知道我是否应该以其他方式提供过滤器。 PS ..当我在浏览器中使用它时,网址工作正常

这是带过滤器的网址的样子:

https://start.jamespro.nl/v4/api/json/tasks/?filter=[{"column":"Date","operator":"=","value":"2017-08-04"}, {"column":"UserId","operator":"=","value":"2"}]

这是我的全部代码:

func apiRequestTasks(url: String) {
        apiRequestHeader(userName: "*******", passWord: "******")
        var running = false
        let urlProjects = NSURL(string: url)
        let task = session?.dataTask(with: urlProjects! as URL) {
            ( data, response, error) in
            if let taskHeader = response as? HTTPURLResponse {
                print(taskHeader.statusCode)
            }
            if error != nil {
                print("There is an error!!!")
                print(error ?? "")
            } else {
                if let content = data {
                    do {
                        let dictionary = try JSONSerialization.jsonObject(with: content) as! [String:Any]
                        print(dictionary)

                    }
                    catch {
                        print("Error: Could not get any data")
                    }
                }
            }
            running = false
        }


        running = true
        task?.resume()

        while running {
            print("waiting...")
            sleep(1)
        }

    }

2 个答案:

答案 0 :(得分:0)

我认为问题在于您创建网址的方式,请尝试以下方式:

let filters = "[\"column\":\"Date\",\"operator\":\"=\",\"value\":\"2017-08-04\"}, {\"column\":\"UserId\",\"operator\":\"=\",\"value\":\"2\"}]"


if var url = URLComponents(string: "https://start.jamespro.nl/v4/api/json/tasks") {
    url.query = "filter=:\(filters)"
    print ("url", url.string ? "invalid url")
}

答案 1 :(得分:0)

我刚编码了我的网址的过滤器部分,但它确实有效。但是对于反应来说就是这样!!!

import numpy as np
import tensorflow as tf
train_data = np.genfromtxt("PERSON1RATING_TRAINING.txt", delimiter="    ")
train_input = train_data[:, :10]
train_input = train_input.reshape(29440, 10)
X_train = tf.placeholder(tf.float32, [29440, 10])

train_target = train_data[:, 10]
train_target = train_target.reshape(29440, 1)
Y_train = tf.placeholder(tf.float32, [29440, 1])

test_data = np.genfromtxt("PERSON1RATING_TEST.txt", delimiter=" ")
test_input = test_data[:, :10]
test_input = test_input.reshape(5120, 10)
X_test = tf.placeholder(tf.float32, [5120, 10])

test_target = test_data[:, 10]
test_target = test_target.reshape(5120, 1)
Y_test = tf.placeholder(tf.float32, [5120, 1])

W_1 = tf.Variable(tf.zeros([10, 100]))
b = tf.Variable(tf.zeros([100]))
H = tf.nn.softmax(tf.matmul(X_train, W_1) + b)
H_test = tf.nn.softmax(tf.matmul(X_test, W_1) + b)

W_2 = tf.Variable(tf.zeros([100, 1]))
Y = tf.nn.softmax(tf.matmul(H, W_2))
Y_obt_test = tf.nn.softmax(tf.matmul(H_test, W_2))

cross_entropy = tf.reduce_mean(-tf.reduce_sum(Y_train * tf.log(Y), 
reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

for _ in range(29440):
    sess.run(train_step, feed_dict={X_train: train_input, 
    Y_train:train_target})

Y = tf.nn.sigmoid(Y)
correct_prediction = tf.equal(tf.round(Y_obt_test), Y_test)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={X_test : test_input, Y_test: test_target}))