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\"}]}}]"

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Tried this model and the API still complains bad data - Stopping it in the debugger shows the same result as C# - String Building Method
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.
No Probs - I will update the main question with the webclient code snippet
You are missing the content type header. Please check the second answer in stackoverflow.com/questions/15091300/…
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 -
|
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);

3 Comments

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?
Removing those I still get a kick back from the server saying it doesn't like it
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
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"
}]
}

Comments

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.