Solved

How to Serailize Object to Array Json C#

Posted on 2016-09-14
7
46 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
Comment Utility
I think you should make Photos a List<product> and then pass it to the JsonConvert.SerializeObject.
1
 

Author Comment

by:karthik80c
Comment Utility
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
Comment Utility
How are you parsing the data in js?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 4

Expert Comment

by:Steven Kribbe
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
oh the response is object am getting
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
We will take a look at the d3.js library for visualizations.  I will provide a walkthrough of a short bar graph example as well as introduce you to the d3.js API.  We also will explore links of other examples and further information regarding SVG's.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now