Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to Serailize Object to Array Json C#

Posted on 2016-09-14
7
56 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
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.

 
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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