Solved

How to Serailize Object to Array Json C#

Posted on 2016-09-14
7
53 Views
Last Modified: 2016-09-15
Hi Experts ,

Am trying to serailize a dynamic object to array . but its not working correctly and here is my code
dynamic product = new JObject();
 foreach (PropertyImage tbl in LST)
                {

                    product.FullImage = SiteUrl + "/" + tbl.Photo;
                    product.imageThumb = SiteUrl + "/" + tbl.Photo;
                    product.description = tbl.Tag;
                    product.Address = Address;
                    product.imageid = tbl.OrderNumber;
                    product.TextColor = "White";

                    if (Photos != "")
                    {
                        Photos += "," + product.ToString();
                    }
                    else
                    {
                        
                        Photos += product.ToString();
                    }
                }
                //Json += Photos;
                // Json += "]";

                var json = JsonConvert.SerializeObject(Photos, Formatting.Indented); ;

Open in new window


but am getting json  output as
"{\r\n  \"FullImage\": \"http://localhost:57226/img/image1.JPG\",\r\n  \"imageThumb\": \"http://localhost
:57226/img/image1.JPG\",\r\n  \"description\": \"Kitchen\",\r\n  \"Address\": \"Test\",\r\n  \"imageid
\": 1,\r\n  \"TextColor\": \"White\"\r\n},{\r\n  \"FullImage\": \"http://localhost:57226/img/image1.JPG
\",\r\n  \"imageThumb\": \"http://localhost:57226/img/image1.JPG\",\r\n  \"description\": \"Bedroom\"
,\r\n  \"Address\": \"Test\",\r\n  \"imageid\": 1,\r\n  \"TextColor\": \"White\"\r\n}"

Open in new window

0
Comment
Question by:karthik80c
  • 4
  • 3
7 Comments
 
LVL 4

Accepted Solution

by:
Steven Kribbe earned 500 total points
ID: 41799286
I think you should make Photos a List<product> and then pass it to the JsonConvert.SerializeObject.
1
 

Author Comment

by:karthik80c
ID: 41799293
Thanks Steven Kribbe that works

[
  {
    "FullImage": "http://localhost:57226/img/image1.JPG",
    "imageThumb": "http://localhost:57226/img/image1.JPG",
    "description": "http://localhost:57226/img/image1.JPG",
    "Address": "Test",
    "imageid": "1",
    "TextColor": "White"
  },
  {
    "FullImage": "http://localhost:57226/img/image1.JPG",
    "imageThumb": "http://localhost:57226/img/image1.JPG",
    "description": "http://localhost:57226/img/image1.JPG",
    "Address": "Test",
    "imageid": "1",
    "TextColor": "White"
  }
]


 . but now js throws error as SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data . Any idea on this
0
 
LVL 4

Expert Comment

by:Steven Kribbe
ID: 41799319
How are you parsing the data in js?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Expert Comment

by:Steven Kribbe
ID: 41799323
Could be that there the data is already parsed by some ajax lib (jQuery) befor it is passed to JSON.parse.
0
 

Author Comment

by:karthik80c
ID: 41799327
am using  
 $.ajax({
       
        url: "Imagehandler.ashx?propertyid=" + propertyid
        
    }).done(function (rsp) {
        debugger;
   var propertyData = JSON.parse(rsp);

Open in new window

0
 
LVL 4

Expert Comment

by:Steven Kribbe
ID: 41799342
The $.ajax may already interpret the http response as JSOn and deliver the rsp as a parsed JSON object. Can you check if the rsp is an object or a string?
0
 

Author Comment

by:karthik80c
ID: 41799343
oh the response is object am getting
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question