3

Currently facing an issue when making a JSON string in C# and trying to send it through to a web API using the WebClient.

Currently I have a few methods, first off the one that works. Using POSTMAN sending the following dataset of text through to the API works correctly:

POSTMAN Dataset - This Works in POSTMAN

{ "record": 
    {
        "form_id": "efe4f66f-b57c-4497-a370-25c0f3d8746a",
        "status": "240",
        "latitude": -82.638039,
        "longitude": 27.770787,
        "form_values": 
        {
            "833b": "99999",
            "683b": "9999999",
            "fa37": "Testing",
            "b2e3": "Testing"
        }
    }
}

In C# I am using a few different ways to construct this sting with little success.

C# List Method - Newtonsoft.Json.Serialization

The first is building a List and then using Newtonsoft.Json to play with it:

    List<Parent> DataList = new List<Parent>();
    List<Child> Form = new List<Child.formvalues>();
    var Formvals = new Child.formvalues
    {
        ActionDescription = Row.ActionDescription,
        ActionNotes = Row.ActionNotes,
        ActionID = Row.ActionID,
        AssetID = Row.AssetID
    };
    Form.Add(Formvals);

    var DataElement = new Parent
    {
        form_id = AppFormID,
        latitude = Lat,
        longitude = Long,
        status = Row.Status,
        form_values = Form
    };
    DataList.Add(DataElement);

    string json = JsonConvert.SerializeObject(DataList.ToArray());

This code results in the following string:

[
    {\"form_id\":\"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
    \"latitude\":-82.638039,
    \"longitude\":27.770787,
    \"status\":239,
    \"form_values\":[
        {\"833b\":99999,
        \"683b\":9999999,
        \"fa37\":\"Testing\",
        \"b2e3\":\"Testing\"
        }]
    }
]

Another attempt I am trying is the following, which in my opinion is the closest so far:

C# - String Building Method

string Content = @"{ "
       + "\"record\": {"
       + "\"form_id\": "\"" + AppFormID + ""\","
       + "\"status\": "\"" + Row.Status + ""\","
       + "\"latitude\": "+ Lat  + ","
       + "\"longitude\": "+ Long + ","
       + "\"form_values\": {"
            + "\"833b\": "\""+Row.AssetID +""\","
            + "\"683b\": "\""+Row.ActionID + ""\","
            + "\"fa37\": "\""+Row.ActionDescription + ""\","
            + "\"b2e3\": "\""+Row.ActionNotes + ""\""
        + "}"
       + "}"
      + "}";

That line results in:

{ \"record\": 
    {
        \"form_id\": \"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
        \"status\": \"239\",
        \"latitude\": -82.638039,
        \"longitude\": 27.770787,
        \"form_values\": 
        {
            \"833b\": \"99999\",
            \"683b\": \"9999999\",
            \"fa37\": \"Testing\",
            \"b2e3\": \"Testing\"
        }
  }
}

The Question!

So the question, is someone able to help me achieving the format in the first JSON that I put into POSTMAN exactly?

UPDATE - 6:40PM

Web client code c# AppURLRef is set in code further up and appears to be correct in the debugger. json is the result of the tests.

var http = new WebClient();
http.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var response = http.UploadString(AppURLRef, "POST", json);

Result of Update

"[{\"record\":{\"form_id\":\"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
\"latitude\":-82.638039,
\"longitude\":27.770787,
\"status\":\"240\",
\"form_values\":         
[{\"833b\":\"99999\",
\"683b\":\"9999999\",
\"fa37\":\"Testing\",
\"b2e3\":\"Testing\"}]}}]"
Caz1224
  • 1,539
  • 1
  • 14
  • 37

3 Answers3

3

Try the following.

public class Record
{
  [JsonProperty(PropertyName = "form_id")]
  public string FormId { get; set; }

  [JsonProperty(PropertyName = "status")]
  public string Status { get; set; }

  [JsonProperty(PropertyName = "latitude")]
  public decimal Latitude { get; set; }

  [JsonProperty(PropertyName = "longitude")]
  public decimal Longitude { get; set; }

  [JsonProperty(PropertyName = "form_values")]
  public Dictionary<string, string> FormValues { get; set; }
}

public class RecordContainer
{
  [JsonProperty(PropertyName = "record")]
  public Record Record { get; set; }
}

Usage:

var container = new RecordContainer();
container.Record = new Record();
// Code to populate values
JsonConvert.SerializeObject(container);

I am using Newtonsoft Json to serialize and I get the same output that you have asked for.

Parthasarathy
  • 2,698
  • 1
  • 12
  • 14
  • Tried this model and the API still complains bad data - Stopping it in the debugger shows the same result as C# - String Building Method – Caz1224 Apr 26 '16 at 07:03
  • Hi @Caz1224, How are you sending the data to Web Api using Web client? If you have a code snippet, I can try to replicate the issue. – Parthasarathy Apr 26 '16 at 07:36
  • No Probs - I will update the main question with the webclient code snippet – Caz1224 Apr 26 '16 at 08:38
  • You are missing the content type header. Please check the second answer in http://stackoverflow.com/questions/15091300/posting-json-to-url-via-webclient-in-c-sharp – Parthasarathy Apr 26 '16 at 09:18
  • Phew, have happy it wasn't so simple. Fixing that issue and I am still left with my issue. Follow your instruction about leaves me with a string: - See Update - – Caz1224 Apr 26 '16 at 10:04
  • What is the signature of your action method in Web Api? – Parthasarathy Apr 26 '16 at 10:33
  • What do you mean by signature of the method in the Web API? – Caz1224 Apr 26 '16 at 10:35
  • I wanted to know the first parameter type of your Web Api method. Is it a string or a custom type or HttpRequestMessage? My assumption is that you use Asp.net Web Api on the server side. Please ignore the comment if you don't use Web Api. – Parthasarathy Apr 26 '16 at 10:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110252/discussion-between-caz1224-and-sarathy). – Caz1224 Apr 26 '16 at 10:52
1

I'd try the following along with NewtonSoft.JSON.

var data = new
{
    record = new
    {
        form_id = "efe4f66f-b57c-4497-a370-25c0f3d8746a",
        status = "240",
        latitude = -82.638039,
        longitude = 27.770787,
        form_values = new Dictionary<string, string>();
    }
}

data.record.form_values["833b"] = "99999";
data.record.form_values["683b"] = "9999999";
data.record.form_values["fa37"] = "Testing";
data.record.form_values["b2e3"] = "Testing";

Then get the JSON:

string json = JsonConvert.SerializeObject(data);
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • The only issue with this is that 833b and 683b can not be variable names as they start with a number. And I assumed you meant JsonConvert.SerializeObject(data); in the usage? – Caz1224 Apr 26 '16 at 10:27
  • Removing those I still get a kick back from the server saying it doesn't like it – Caz1224 Apr 26 '16 at 10:34
  • I had to scrap the WebClient() it just would not work. I put RestClient in and then formed the JSON with your method and it works perfectly. Thanks for the help – Caz1224 Apr 26 '16 at 12:32
0

I think it is much similar to this One. you can try the following code to achhieve the requirement:

var dd = {
"FirstName": "ABC",
"username": "abc123",
"password": "abc@123",
"Cnumbers": [{
    "Home": "0987654321"
}, {
    "Company": "7654321"
}]
}
Community
  • 1
  • 1
Ram Singh
  • 6,664
  • 35
  • 100
  • 166