0

I don't know if this is me or the JSON that I am getting. Anyhow I was reading this other article which I thought was relevant but no idea what the guys was talking about when he figured out what he needed to to more json c# issues

here is a sample of the JSON I am getting now

{
    "totalResult": 2,
    "merchants": {
        "1718167": {
            "merchantRank": "29",
            "merchantName": "bob the builders factory",
            "merchantSlogan": "if he can't fix it we can",
            "merchantBdesc": "",
            "merchantLogo": "pic1.gif",
            "merchantLink": "http:\/\/www.randomlink.com\/",
            "merchantAddress": "place
street St
area
city
1111",
            "merchantLat": "-15.9935425",
            "merchantLng": "58.0836955",
            "merchantGeoLevel": "st",
            "merchantDistance": "0.00",
            "merchantCategories": "builder",
            "merchantEmail": "",
            "merchantUrl": "http:\/\/www.randomlink.com\/",
            "merchantPhone": "0123456789",
            "merchantMobile": "",
            "merchantSrc": "AJF"
        },
        "113711": {
            "merchantRank": "229",
            "merchantName": "A A A Builders",
            "merchantSlogan": "",
            "merchantBdesc": "",
            "merchantLogo": "pic26.gif",
            "merchantLink": "http:\/\/www.randomlink.com\/",
            "merchantAddress": "",
            "merchantLat": "",
            "merchantLng": "",
            "merchantGeoLevel": "",
            "merchantDistance": "0.00",
            "merchantCategories": "Builder",
            "merchantEmail": "[email protected]",
            "merchantUrl": "http:\/\/randomlink.com",
            "merchantPhone": "0123456789",
            "merchantMobile": "",
            "merchantSrc": "GHF"
        }
    }
}

for the sake of sanity I have reduced the number of results, anyways, so I was attempting to build the data contracts for this and I came into an issue that I had previously, where some clever trickery got me the results that I need. However this time its different...

Using the [DataContract] I can get the totalResult, however, to get merchants I keep getting errors... and I believe that because the "113711", and the "113711" are dynamic this causes a problem to create a data model. So I thought that perhaps a dictionary would solve this problem. However whenever I try it I get errors!!

Here is what I tried to use:

[DataContract]
public class result
{
    [DataMember(Name = "totalResult")]
    public string totalResult { get; set; }
    [DataMember(Name = "merchants")]
    public Dictionary<string, metchant_info> merchants { get; set; }
}
[DataContract]
public class metchant_info
{
    [DataMember(Name = "merchantRank")]
    public string merchantRank;
    [DataMember(Name = "merchantName")]
    public string merchantName;
    [DataMember(Name = "merchantSlogan")]
    public string merchantSlogan;
    [DataMember(Name = "merchantBdesc")]
    public string merchantBdesc;
    [DataMember(Name = "merchantLogo")]
    public string merchantLogo;
    [DataMember(Name = "merchantLink")]
    public string merchantLink;
    [DataMember(Name = "merchantAddress")]
    public string merchantAddress;
    [DataMember(Name = "merchantLat")]
    public string merchantLat;
    [DataMember(Name = "merchantLng")]
    public string merchantLng;
    [DataMember(Name = "merchantGeoLevel")]
    public string merchantGeoLevel;
    [DataMember(Name = "merchantDistance")]
    public string merchantDistance;
    [DataMember(Name = "merchantCategories")]
    public string merchantCategories;
    [DataMember(Name = "merchantEmail")]
    public string merchantEmail;
    [DataMember(Name = "merchantUrl")]
    public string merchantUrl;
    [DataMember(Name = "merchantPhone")]
    public string merchantPhone;
    [DataMember(Name = "merchantMobile")]
    public string merchantMobile;
    [DataMember(Name = "merchantSrc")]
    public string merchantSrc;
}

and here is the code behind...

            try
            {
                Stream responseStream = e.Result;
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(result));
                result response = (result)ser.ReadObject(responseStream);
            }
            catch (Exception ex)
            {
                return;
            }

The error I am getting currently is :

"Could not evaluate expression"

if you want the stack trace I will post it, but its a rather large amount of useless information... Also if you change the datamember Merchants to a string you will see the following error:

"System.Collections.Generic.Dictionary`2[System.String,System.Object]"

So... any thoughts?

5
  • Regarding - "System.Collections.Generic.Dictionary`2[System.String,System.Object]" where is the rest of this error? May 2, 2012 at 12:33
  • Please use a better title for your question
    – Tarion
    May 2, 2012 at 12:36
  • @Stuart.Sklinar that is what is returned in the string, there is no further information
    – Ghostfire
    May 2, 2012 at 13:06
  • @Tarion what would you prefer the title to be?
    – Ghostfire
    May 2, 2012 at 13:06
  • @Ghostfire (RE: @Tarion) Something that related directly to THIS question, and not something that could be related to another question :) Oh and something descriptive about the question! May 2, 2012 at 13:52

2 Answers 2

3

Using Json.Net, you can handle the case dynamically as below

var obj = (JObject)JsonConvert.DeserializeObject(json);

foreach (var item in obj["merchants"])
{
    Console.WriteLine(item.First()["merchantName"]);
}

or you can deserialize each merchant to your metchant_info object in a loop

foreach (var item in obj["merchants"])
{
    var m = JsonConvert.DeserializeObject<metchant_info>(item.First().ToString());
    Console.WriteLine(m.merchantName);
}
0
0

I dont have a working solution but after a bit of research, I found the following information:

Looking at this Question: How would I read into a 'nested' Json file with 'DataContractJsonSerializer' in C# .NET (win7 phone)? the problem seems to be the

public Dictionary<string, metchant_info> merchants { get; set; }

There seems to be an issue with the way the Serializer handles Dictionaries: Deserialization problem with DataContractJsonSerializer

Any way to make DataContractJsonSerializer serialize Dictionaries properly?

Hope you can figure it out.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.