Caz1224
Caz1224

Reputation: 1569

C# JSON String to Web API

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

Upvotes: 3

Views: 1199

Answers (3)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

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);

Upvotes: 1

Parthasarathy
Parthasarathy

Reputation: 2818

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.

Upvotes: 3

Ram Singh
Ram Singh

Reputation: 6918

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

Upvotes: 0

Related Questions