3

Thanks in advance for any assistance!

I'm trying to parse a Json result from an API and the returned result of the data seems a bit difficult for me to grasp.

  • It appears to return a 'header' and then the items (...see raw result example).
  • I tried to parse this using Json.NET

I am not familiar with Json.NET nor with Json for that matter.

  • How do i parse the results and map it to a class?
  • Either using JSON.Net or another tool.

Thanks

Raw Returned Result (Json)

{
"code": 0,
"message": "Successful",
"partials": {
    "_key": "partial",
    "0": {
        "datetime": "2011-03-08 16:22:51",
        "customer_id": "373263",
        "domain": "xyz.com ** deleted 2011-04-08 18:26:55 UTC**",
        "name": "Joe Customer",
        "phone": "1234567894",
        "email": "[email protected]",
        "offer": "",
        "pub_id": "",
        "sub_id": "",
        "data1": "", ... , "data3": "",
        "custom1": "", ..., "custom8": "" 
    },
    "1": {
        "datetime": "2011-03-08 16:43:11",
        "customer_id": "373296",
        "domain": "abc.com ** deleted 2011-04-08 18:26:55 UTC**",
        "name": "Jane Customer",
        "phone": "1234567891",
        "email": "[email protected]",
        "offer": "",
        "pub_id": "",
        "sub_id": "",
        "data1": "", ... , "data3": "",
        "custom1": "", ..., "custom8": "" 
    }
} 

}

My Attempt

        HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();

        if (request.HaveResponse == true)
        {
            Stream responseStream = webResponse.GetResponseStream();
            StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            string responseString = responseReader.ReadToEnd();

            JObject o = JObject.Parse(responseString);

            IList<JToken> results = o["partials"].Children().ToList();

            IList<bbParial> oResults = new List<bbPartial>();

            foreach (JToken t in results)
            {
                if (t.ToString().Contains("partial"))
                {
                    // Do nothing this is the header
                }
                else
                {
                    bbPartial bbp = JsonConvert.DeserializeObject<bbPartial>(t.ToString());
                }
            }
        }

Sample Class

public class bbpartial
{
    public string _key;
    public string datetime;
    public string customer_id;
    public string domain;
    public string name;
    public string phone;
    public string email;
    public string offer;
    public string pub_id;
    public string sub_id;
    public string data1;
    public string data2;
    public string data3;
    public string custom1;
    public string custom2;
    public string custom3;
    public string custom4;
    public string custom5;
    public string custom6;
    public string custom7;
    public string custom8;
}
6
  • Could you post some of the code you've tried already? Commented May 2, 2011 at 16:37
  • Could you post an example of the class you want to populate also. Commented May 2, 2011 at 16:40
  • Have you tried the JavaScriptSerializer in the .NET framework? msdn.microsoft.com/en-us/library/bb355316.aspx Commented May 2, 2011 at 17:07
  • see also, stackoverflow.com/questions/4523410 - Why would I ever use anything other than JavaScriptSerializer? Commented May 2, 2011 at 17:14
  • Thanks goalie, this is actually a winforms application...sorry, should have mentioned that up front. Commented May 2, 2011 at 17:15

1 Answer 1

2
JObject jsonObj = JObject.Parse(jasonExample);

Customer customerOne = new Customer()
 {
   Name =(string)jsonObj.selectToken("partials[0].name")
 }

Does that work? Is it helpful?

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

Comments

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.