1

I am trying to parse a JSON object passed from a WCF Service, here is the Response the service gives

[
 {
   "Caption": "Hello",
   "CityState": "Seattle",
   "URL": "Test"
 },
 {
   "Caption": "World",
   "CityState": "Chicago",
   "URL": "Test"
 },
 {
    "Caption": "Hello",
    "CityState": "New York",
    "URL": "Test"
 }
]

Here is the WCF Code to create the object

///Custom Object has 3 string properties
List<OW_CompanyACSearch> search = new List<OW_CompanyACSearch>();

  DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<OW_CompanyACSearch>));
        using (MemoryStream stream = new MemoryStream())
        {
            serializer.WriteObject(stream, search);
            return Encoding.Default.GetString(stream.ToArray());
        }

and here is the JQuery that I am trying to make work

    source: function (request, response) {
            j$.ajax({
                url:"testservice.svc",
                dataType: "json",
                data: {
                    Search: request.term
                },
                success: function (data) {

                    response(j$.map(data, function (item) {
                        return {
                            label: item.Caption,
                            value: item.Caption,
                            URL: item.URL,
                            CityState: item.CityState
                        }

                    }));
                }
            });
        }

The issue that I am having is that when I fall into the return to parse the members on the object, none of the properties are defined. If I drill down into the object, it seems that it is treating object as one long string, so index[0] would be "[" and so on.

I have read all over and I have tried every option that I have seen and I still get this issue. I am not sure if I am serializing or parsing incorrectly, but any help would be appreciated.

Also, not sure if it matters, but the binding on the WCF is webHttpBinding

3
  • it IS coming back as a string, isn't it? you need to call JSON.parse() on it first to convert the response to a JSON object I believe. Commented Jun 7, 2013 at 19:45
  • 1
    Another option would be to call data = j$.parseJSON(data); right before the part with response(j$.map(data, ... Commented Jun 7, 2013 at 19:47
  • @jonhopkins when I add the line above the response, data becomes a null object. thoughts? Commented Jun 7, 2013 at 20:36

2 Answers 2

1

Wow this was awesome, come to find out that data was just a wrapper for the JSON object, and there was a property named "d" that was the actual object.

So this

 data = j$.parseJSON(data.d);

filled data with the object, and I could move forward.

Sign up to request clarification or add additional context in comments.

Comments

0

The data object should already be a variable array that contains your data at the properties as named by the JSON. You should be able to do something like this:

source: function (request, response) {
        j$.ajax({
            url:"testservice.svc",
            dataType: "json",
            data: {
                Search: request.term
            },
            success: function (data) {
                alert(data[0].Caption);
            }
        });
    }

To verify the object structure.

4 Comments

that is the issue, the data object coming back is not indexablem, so data[0] is null
Make sure that the testservice.svc's response type is the JSON MIME type of "application/json". This is the only thing that can be going wrong. since dataType: "json" tells jQuery to parse the data as a JSON object.
how exactly do I do that? Is there a property I need to set? I figured returning the encoded string is enough.
@IsaacLevin this link might help you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.