0

I am trying to convert a json string (content) response

to parse into Attachment [] object using

Attachment attachment = JsonConvert.DeserializeObject<Attachment>(content);

Question: How do I turn it into an Attachment [] object array?

4
  • what is your question? Commented Jan 11, 2016 at 18:10
  • 4
    And? Did it work? Did it not? If not, what's the error? Commented Jan 11, 2016 at 18:11
  • Its not working. How do I turn it into an Attachment [] object array? Commented Jan 11, 2016 at 18:16
  • json2csharp.com this site will generate a proper C# Class for you . Commented Jan 11, 2016 at 18:25

3 Answers 3

2

Your Attachment object doesn't quite reflect the data structure of the response. Create an AttachmentCollection to contain the array of attachments:

public class AttachmentCollection
{
    public List<Attachment> Attachment { get; set; }
}

Then deserialize as an AttachmentCollection:

var attachments = JsonConvert.DeserializeObject<AttachmentCollection>(content);
Sign up to request clarification or add additional context in comments.

6 Comments

Is it possible to convert this into IEnumerable so that the content can be passed by WebApi.
Can you elaborate? Are you trying to return this data in a response?
Yes since I'm getting that json from another web service and want to parse it and then resend it as json response so it can be read using Angularjs
In that case it's not necessary to distinguish between a List/IEnumerable. The JSON serializer will convert both to a JSON array.
getting content = client.DownloadString(url);
|
0

You can also use list(Collection) for that.

public class RootObject
{
    public List<Attachment> Attachment { get; set; }
}

1 Comment

Good point about the List -- in retrospect I think JsonConvert needs a concrete type instead of an IEnumerable.
0

You could use JSON.Net

What you need to do is first get the JSONArray and do a for statement for each JSONObject in the JSON Array. In the for statement you could use Linq to create a list of your custom Attachement class. See below:

List<Attachment> AttachmentList = new List<Attachment>();
JArray a = JArray.Parse(jsonString);

foreach (JObject o in a.Children<JObject>())
{
    Attachment newAttachment = new Attachment
    {
       WriteUpID = (int)o["WriteUpID"],
       InitialScanID = (int)o["InitialScanID"],
       //etc, do this for all Class items. 
    }
    AttachmentList.Add(newAttachment);
}
//Then change the AttachmentList to an array when you're done adding Attachments
Attachment[] attachmentArray = AttachmentList.ToArray();  

Voila

1 Comment

[JsonReaderException: Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1.]

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.