Hello experts,
I made a custom connector which returns me plain text data.
I am trying to convert this data to JSON before returning. I am trying this with the code you can add to the custom connector. It's a first time for me trying to do this so I am running in a error I can not seem to resolve.
Error:
{
"error": {
"message": "Input string was not in a correct format.",
"stackTrace": [
"at Void ThrowOverflowOrFormatException(ParsingStatus, System.TypeCode) : Line 0",
"at Void MoveNext() : Line 0",
"at Void Throw() : Line 0",
"at Void ThrowForNonSuccess(System.Threading.Tasks.Task) : Line 0",
"at Void HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) : Line 0",
"at Void MoveNext() : Line 754",
"at Void Throw() : Line 0",
"at Void ThrowForNonSuccess(System.Threading.Tasks.Task) : Line 0",
"at Void HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) : Line 0",
"at Void MoveNext() : Line 673"
]
}
}
My code:
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text.Json;
public class Script : ScriptBase
{
public class Skill
{
public string Name { get; set; }
public int Rank { get; set; }
public int Level { get; set; }
public int Experience { get; set; }
}
public override async Task < HttpResponseMessage > ExecuteAsync()
{
// Check which operation ID was used
if (this.Context.OperationId == "GetStatsForRegularPlayer")
{
return await this.GetStatsForRegularPlayerAndTransformOperation().ConfigureAwait(false);
}
// Handle an invalid operation ID
HttpResponseMessage response = new HttpResponseMessage(
// HttpStatusCode.BadRequest
// HttpStatusCode.BadRequest
);
response.Content = CreateJsonContent(
$"Unknown operation ID '{this.Context.OperationId}'"
);
return response;
}
private async Task < HttpResponseMessage > GetStatsForRegularPlayerAndTransformOperation()
{
// Use the context to forward/send an HTTP request
HttpResponseMessage response = await this.Context.SendAsync(
this.Context.Request,
this.CancellationToken
).ConfigureAwait(continueOnCapturedContext: false);
// Do the transformation if the response was successful
if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(
continueOnCapturedContext: false
);
string[] stats = responseString.Split(' ');
int counter = 0;
List<Skill> skills = new List<Skill>();
List<string> skillNames = new List<string>{ "Overall", "Attack", "Defence", "Strength", "Hitpoints", "Ranged", "Prayer", "Magic", "Cooking", "Woodcutting", "Fletching", "Fishing", "Firemaking", "Crafting", "Smithing", "Mining", "Herblore", "Agility", "Thieving", "Slayer", "Farming", "Runecrafting", "Hunter", "Construction" };
foreach (var skill in stats)
{
counter++;
Skill skill1 = new Skill();
List<string> skillValues = new List<string>(skill.Split(','));
skill1.Name = skillNames[counter - 1];
skill1.Rank = Convert.ToInt32(skillValues[0]);
skill1.Level = Convert.ToInt32(skillValues[1]);
skill1.Experience = Convert.ToInt32(skillValues[2]);
skills.Add(skill1);
if (counter == 24) break;
}
JObject JSon = new JObject();
JSon["stats"] = JToken.FromObject(skills);
response.Content = CreateJsonContent(JSon.ToString());
}
return response;
}
}
I simulated my code locally and this is the JSON format it outputs:
[
{
"Name": "Overall",
"Rank": 32723,
"Level": 2244,
"Experience": 314982696
},
{
"Name": "Attack",
"Rank": 226033,
"Level": 99,
"Experience": 13070710
},
{
"Name": "Defence",
"Rank": 199665,
"Level": 99,
"Experience": 13063662
},
{
"Name": "Strength",
"Rank": 92397,
"Level": 99,
"Experience": 18909582
},
{
"Name": "Hitpoints",
"Rank": 103839,
"Level": 99,
"Experience": 26908884
},
{
"Name": "Ranged",
"Rank": 106838,
"Level": 99,
"Experience": 22719690
},
{
"Name": "Prayer",
"Rank": 131108,
"Level": 90,
"Experience": 5575401
},
{
"Name": "Magic",
"Rank": 93190,
"Level": 99,
"Experience": 15959549
},
{
"Name": "Cooking",
"Rank": 267483,
"Level": 98,
"Experience": 11969621
},
{
"Name": "Woodcutting",
"Rank": 85175,
"Level": 99,
"Experience": 13053236
},
{
"Name": "Fletching",
"Rank": 170853,
"Level": 97,
"Experience": 10717196
},
{
"Name": "Fishing",
"Rank": 20636,
"Level": 99,
"Experience": 15053096
},
{
"Name": "Firemaking",
"Rank": 94580,
"Level": 99,
"Experience": 13107338
},
{
"Name": "Crafting",
"Rank": 6292,
"Level": 99,
"Experience": 15765877
},
{
"Name": "Smithing",
"Rank": 66788,
"Level": 94,
"Experience": 8342943
},
{
"Name": "Mining",
"Rank": 32374,
"Level": 99,
"Experience": 13321133
},
{
"Name": "Herblore",
"Rank": 76842,
"Level": 93,
"Experience": 7569384
},
{
"Name": "Agility",
"Rank": 50034,
"Level": 95,
"Experience": 8891762
},
{
"Name": "Thieving",
"Rank": 32030,
"Level": 99,
"Experience": 13721945
},
{
"Name": "Slayer",
"Rank": 128720,
"Level": 96,
"Experience": 10451956
},
{
"Name": "Farming",
"Rank": 23482,
"Level": 99,
"Experience": 20266397
},
{
"Name": "Runecrafting",
"Rank": 28780,
"Level": 99,
"Experience": 13063026
},
{
"Name": "Hunter",
"Rank": 57530,
"Level": 96,
"Experience": 10375374
},
{
"Name": "Construction",
"Rank": 29721,
"Level": 99,
"Experience": 13104934
}
]

Report
All responses (
Answers (