1

早上好,有人可以帮我看看这个,看看为什么我只能得到回复

 {"d":"success : False, message  : "}

理想情况下,我希望得到回应。

 {"success" : true, "message" : "***Message Here***"}

下面是我的代码...

 [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string CheckFeedSubmission()
    {
        string responseText = "";
        bool responseSuccess = false;
        try
        {
            aboDataDataContext dc = new aboDataDataContext();

            List<string> id = (from fl in dc.feedLogs where fl.feedStatus == "_SUBMITTED_" select fl.feedSubmissionId).ToList();

            foreach (var submissionId in id)
            {
                try
                {
                    MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();

                    config.ServiceURL = "https://mws.amazonservices.co.uk";
                    config.SetUserAgentHeader("twe-amazon-mws", "1.0", "c#");

                    MarketplaceWebService.MarketplaceWebService service = new MarketplaceWebServiceClient(accesskeyID, secretkey, config);

                    GetFeedSubmissionResultRequest request = new GetFeedSubmissionResultRequest();

                    MemoryStream ms = new MemoryStream();

                    request.Merchant = merchantID;
                    request.Marketplace = marketids[0];
                    request.FeedSubmissionId = submissionId;
                    request.FeedSubmissionResult = ms;

                    try
                    {
                        GetFeedSubmissionResultResponse resp = service.GetFeedSubmissionResult(request);

                        if (resp.IsSetGetFeedSubmissionResultResult() == false)
                        {
                            responseText = "IsSetGetFeedSubmissionResultResult=false";
                            responseSuccess = false;
                        }

                        else
                        {
                            StreamReader sr = new StreamReader(ms);
                            string srs = sr.ReadToEnd();

                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(srs);

                            try
                            {
                                feedLog feedUpdate = dc.feedLogs.Single(l => l.feedSubmissionId == submissionId);
                                feedUpdate.feedStatus = "_" + doc.SelectSingleNode("/AmazonEnvelope/Message/ProcessingReport/StatusCode").InnerText.ToUpper().ToString() + "_";
                                feedUpdate.totalProcessed = Convert.ToInt32(doc.SelectSingleNode("/AmazonEnvelope/Message/ProcessingReport/ProcessingSummary/MessagesProcessed").InnerText);
                                feedUpdate.sucessfulPublish = Convert.ToInt32(doc.SelectSingleNode("/AmazonEnvelope/Message/ProcessingReport/ProcessingSummary/MessagesSuccessful").InnerText);
                                feedUpdate.publishwithErrors = Convert.ToInt32(doc.SelectSingleNode("/AmazonEnvelope/Message/ProcessingReport/ProcessingSummary/MessagesWithError").InnerText);
                                feedUpdate.publishwithWarning = Convert.ToInt32(doc.SelectSingleNode("/AmazonEnvelope/Message/ProcessingReport/ProcessingSummary/MessagesWithWarning").InnerText);
                                feedUpdate.feedResponseXML = doc.InnerXml.ToString();

                                if (doc.SelectSingleNode("/AmazonEnvelope/Message/ProcessingReport/StatusCode").InnerText.ToUpper().ToString() == "COMPLETE")
                                {
                                    try
                                    {
                                        dc.tweProducts.Where(p => p.feedSubmitId == submissionId).ToList().ForEach(i => i.prodPublished = 1);
                                    }
                                    catch (Exception ee) { responseSuccess = false; responseText = "db update tweProducts exception:" + ee.Message; }
                                }

                                dc.SubmitChanges();
                            }
                            catch (Exception ex) { responseSuccess = false; responseText += "\n db update feedLog exception : " + ex.Message; }

                            XDocument pxerror = XDocument.Parse(srs);
                            var results = pxerror.Root.Descendants("Result")
                                          .Where(p => p.Element("ResultCode").Value == "Error");
                            if (results != null)
                            {
                                foreach (var t in results)
                                {
                                    publishError perrors = new publishError();
                                    {
                                        try
                                        {
                                            var resultCode = t.Element("ResultMessageCode").Value;
                                            var resultDescription = t.Element("ResultDescription").Value;
                                            var sku = t.Element("AdditionalInfo").Element("SKU").Value;

                                            perrors.sku = sku;
                                            perrors.errorId = resultCode;
                                            perrors.description = resultDescription;
                                            perrors.feedSubmissionId = submissionId;
                                            perrors.errorResolved = false;

                                            dc.publishErrors.InsertOnSubmit(perrors);
                                            dc.SubmitChanges();
                                        }
                                        catch (Exception ex) { responseSuccess = false; responseText += "db update publishError exception: " + ex.Message; }
                                    }
                                }
                            }

                            #endregion

                            responseSuccess = true;
                            responseText = "Complete";
                        }

                    }
                    catch (Exception ex) { responseSuccess = false; responseText = "GetFeedSubmissionResultResponse error" + ex.Message; }
                }
                catch (MarketplaceWebServiceException ex) { responseSuccess = false; responseText = "starts MarketplaceWebServiceConfig, exception message:" + ex.Message + ",status code:" + ex.StatusCode; }

            }
        }
        catch (Exception ex) { responseSuccess = false; responseText = "starts at aboDataDataContext, exception message:" + ex.Message; }

        return "success : " + responseSuccess + ", message  : " + responseText ;
    }
}

这是我之前的问题的后续 - How to return JSON from webservice

另外,如果您看到任何改进代码的方法,请告诉我:)

非常感谢所有帮助,在此先感谢。

4

2 回答 2

2

这是因为您返回的是单个字符串而不是返回对象。您想要返回的 Json 格式表示存在一个具有 2 个属性“Success”和“Message”的对象(而您正在返回一个简单的字符串)。

首先制作一个将返回的对象

[DataContract]
public class Message
{
    [DataMember]
    public string Success { get; set; }
    [DataMember]
    public string Message { get; set; }
}

更改方法的签名

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Message CheckFeedSubmission()
    {
    .
    .
    }

然后返回这个对象

return new Message() { Success = responseSuccess , Message = responseText };
于 2012-07-12T09:37:08.940 回答
0

根据定义,webmethod 总是返回一个看起来像 { d : object } 的 json,其中 object 是你输入的任何内容,所以我认为你不能避免“d”

所以你需要返回类似 "{"success" : true, "message" : " Message Here "}"

不确定它是否会导致语法问题,所以也许你最好返回一个像“[{成功:真,消息:”msg“}]”这样的json arr

于 2012-07-12T09:37:18.183 回答