1

Hello I am facing a very simple problem but it is not getting Solved. Here is my class design

public class Program
{
    public string ProgramName { get; set; }
    public string ProgramTime { get; set; }
    public string ProgramDetails { get; set; }
}

public class Listing
{
    public string ChannelName { get; set; }
    public string NowShowing { get; set; }
    public string NowShowingTime { get; set; }
    public string NowShowingDescription { get; set; }
    public string NowShowingPicture { get; set; }
    public List<Program> Programs { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public string about { get; set; }
    public List<Listing> listings { get; set; }
}

I am parsing using the following code.

JObject json = JsonConvert.DeserializeObject(e.Result) as JObject;
Listing ls = new Listing
{
    ChannelName = (string)json["listings"].Children()["ChannelName"],
    NowShowing = (string)json["listings"].Children()["NowShowing"],
    Programs = new Program
    {
         ProgramName = (string)json["listings"]["Program"]["ProgramName"]
    }
};

Help me solve my lame approach. My concerns are parsing the items correctly and also how to add them to the nested list "Programs". The second one is more crucial.

Sample Json input-

   {
      "listings": [
      {
          "ChannelName": "NTV BANGLA",
          "NowShowing": "Ei Shomoy (R)",
          "NowShowingTime": "12:10",
          "NowShowingDescription": "Ei Shomoy is a daily talk show ........",
          "Programs": [
          {
              "ProgramName": "Ainer Chokhe (R)",
              "ProgramTime": "13:00",
              "ProgramDetails": "Human Rights and law based program Ainer Chokhe,"
          },
          {
              "ProgramName": "Shonkhobash",
              "ProgramTime": "15:10",
              "ProgramDetails": "Drama serial Shonkhobash, script by Bipasha Hayat and"
          }
        ]
      },
      {
          "ChannelName": "CHANNEL i",
          "NowShowing": "Taroka Kothon (Live)",
          "NowShowingTime": "12:30",
          "NowShowingDescription": "City Cell Taroka Kothon Live is a talk show ",
          "Programs": [
          {
               "ProgramName": "Channel i Top News",
               "ProgramTime": "13:00",
               "ProgramDetails": "Mutual Trust Bank top news (Shirsho Shongbad)"
       },
       {
               "ProgramName": "Ebong Cinemar Gaan",
               "ProgramTime": "13:10",
               "ProgramDetails": "Ebong Cinemar Gaan, a musical show based on "
        }
 ]
         }
      ]
   }

EDIT1

var customers = JsonConvert.DeserializeObject<RootObject>(e.Result);
Listing ls = new Listing
{
       ChannelName = customers.listings.First().ChannelName,
       NowShowing=customers.listings.First().NowShowing,
       Programs=??
};
4
  • 1
    You have to iterate over the nested arrays to create your Programs. Commented Jan 8, 2014 at 11:57
  • can you provide sample json for input? Commented Jan 8, 2014 at 12:03
  • your parsing code without runtime error for you sample json? Commented Jan 8, 2014 at 14:37
  • can you provide sample expected output for your json input? Commented Jan 8, 2014 at 14:42

2 Answers 2

1

if e.Result is string with your JSON try this

var jss = new JavaScriptSerializer();
var o = jss.Deserialize<RootObject>(e.Result);

UPDATE
possibly you need something like this

var customers = JsonConvert.DeserializeObject<RootObject>(e.Result);
Listing ls = new Listing
{
    ChannelName = customers.listings.First().ChannelName,
    NowShowing=customers.listings.First().NowShowing,
    Programs=customers.listings.First().Programs
};

UPDATE2
if you want based on existing you can try comething like this

var customers = JsonConvert.DeserializeObject<RootObject>(e.Result);
Listing ls = new Listing
{
    ChannelName = customers.listings.First().ChannelName,
    NowShowing=customers.listings.First().NowShowing,
    Programs=customers.listings.First().Programs.Select(p=>new Program{
                                        ProgramName=p.ProgramName,
                                        ProgramTime=p.ProgramTime, 
                                        ProgramDetails = p.ProgramDetails
                                       }).ToList()
};

UPDATE3
or if you whant simply random you can try something like this

var customers = JsonConvert.DeserializeObject<RootObject>(e.Result);
Listing ls = new Listing
{
    ChannelName = customers.listings.First().ChannelName,
    NowShowing=customers.listings.First().NowShowing,
    Programs=Enumerable.Range(1,10).Select(p=>new Program{
                                        ProgramName="generated name",
                                        ProgramTime="generated time", 
                                        ProgramDetails = "generated details"
                                       }).ToList()
};
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry I could not express myself properly, I have data in my hands, I want to assign them to the correct properties for later use.How can I populate ChannelName, NowShowing and most importantly nested array Programs?
I don't quite understand what you mean, in o you have deserialized object with filed propertise include List<Listing>,List<Program>, so you can simply get it annd change, can you provide sample what you concrete mean?
I have already solved the first part of my question(I'll elaborate them in my answer). Please tell me how to insert data into "programs"? I'm having trouble because it is a list, not a property like "ChannelName" or "NowShowing"
This is a correct approach. But I need the values inside "ProgramName", "ProgramTime" , "ProgramDetails" too.
There is some casting problem. An IEnumerable to Generic conversion error has been thrown.
|
0

Use DataContractJsonSerializer to parse json string in windows phone.

MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(e.Result));

DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(List<RootObject>));
RootObject itemDataList = dataContractJsonSerializer.ReadObject(memoryStream) as RootObject;

ChannelName = itemDataList.listings.First().ChannelName;

4 Comments

it is saying illegal characters in path
unfortunately, itemdatalist remains empty. am I missing something?
I made edit in my ans please check it and let me know if it works
JObject json = JsonConvert.DeserializeObject(e.Result) as JObject; I am getting my json data from here. The problem lies in the parsing and iteration.

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.