skip to Main Content

I am using shopify iOS SDK(mobile-buy-sdk-ios) in react native to get login user’s orders.
Here is my code,

let query = Storefront.buildQuery { $0
        .customer(customerAccessToken: token) { $0
          .orders(first: count, reverse: true) { $0
                .edges { $0
                    .node { $0
                      .id()
                      .orderNumber()
                      .totalPrice()
                      .statusUrl()
                      .lineItems(first: 25){ $0
                          .edges{ $0
                              .node{ $0
                                  .title()
                                  .quantity()
                                  .variant{ $0
                                      .id()
                                      .price()
                                      .title()
                                      .image{ $0
                                          .originalSrc()
                                      }
                                  }
                              }
                          }
                      }


    let task  = self.client.queryGraphWith(query, cachePolicy: .networkOnly) { response, error in
     error.debugUserPrint()
     let userOrders = response.customer?.orders.edges[0].node;
     let res = try! JSONSerialization.data(withJSONObject: userOrders)
     completion([res])
}

And I am getting this response in userOrders variable

<QueryRoot: ["customer": {
    orders =     {
        edges =         (
                        {
                node =                 {
                    id = "Z2lkOi8vc2hvcGlmeS9PcmRlci8yMjY2NTM3NzU0NzEwP2tleT0zNWFiYzBkMjRmMDk3MjZlYzgzYjkwZDVlZGI5YjM4MA==";
                    lineItems =                     {
                        edges =                         (
                                                        {
                                node =                                 {
                                    quantity = 1;
                                    title = "Gift wrapping";
                                    variant =                                     {
                                        id = "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMjE3MzkzNjYyMzcwMg==";
                                        image =                                         {
                                            originalSrc = "https://cdn.shopify.com/s/files/1/2331/3377/products/Gift_Boxes_11_22_2017_Standard_1024x1024_60d01a1c-f665-4c9e-b80b-f6fda9167de3.jpg?v=1521444032";
                                        };
                                        price = "10.00";
                                        title = "Default Title";
                                    };
                                };
                            }
                        );
                    };
                    orderNumber = 1040;
                    statusUrl = "23313377/orders/11f378e7df2731521429f377015d2ec2/authenticate?key=35abc0d24f09726ec83b90d5edb9b380";
                    totalPrice = "10.00";
                };
            }
        );
    };
}]>)

this formate, so try to parse this data to JSON object to pass data from iOS function to javascript function. I have tried

JSONSerialization.data(withJSONObject: userOrders)

but it is not working. I just want to parse this data to JSON. I have also tried many other ways but no luck.

Thanks.

2

Answers


  1. They’re a number of ways I can think of…

    1. Alamofire if you have access to that query endpoint
    Alamofire.request("http://feeds.news24.com/articles/Fin24/Tech/rss").responseJSON { json in 
    /// do what you want with your json
    }
    
    1. No endpoint, use SwiftyJSON
    let json = JSON(data: dataFromNetworking)
    if let userName = json[0]["user"]["name"].string {
      //Now you got your value
    }
    
    Login or Signup to reply.
  2. I don’t think there is straightforward way to get JSON, using mobile-buy-sdk. However you can convert response to JSON

    if let data = try? JSONSerialization.data(withJSONObject: userOrders.fields, options: .prettyPrinted) {
                    print(String(data: data, encoding: .utf8))
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search