0

I created an array and I want to send it to a C# application. I want to be able to use it as a C# string array so I can create a for loop where I can run some tasks.

Node.js (Express.js)

router.get('/arr', function(req,res,next) {
  var arr = ["orange", "plum", "strawberry", "lemon", "tangerine"];
  res.send(arr);
});

C# code.

public string Arr() {
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.0.2.2/arr");
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  StreamReader stream = new StreamReader(response.GetResponseStream());
  string final_response = stream.ReadToEnd();
  stream.Dispose();
  return final_response;
 }

C# application's editor (This is an editor where I can do very limited stuff, so there's no way to import 3rd party packages but you can do basic stuff like http get/post, etc)

string Answer = Lib.Arr();

How can I create Answer as a string array? I tried creating Answer as string[] Answer but it says string can't get converted into string[], where am I wrong?

I'm new to C#.

1

1 Answer 1

1

If all you need to parse is an array of strings, then you can do something like this:

string trimmedAnswer = Lib.Arr().Trim(); // Remove whitespaces from the beginning and end
trimmedAnswer = trimmedAnswer.Substring(1, trimmedAnswer.Length - 1); // Remove [ and ] characters
string[] Answer = trimmedAnswer.Split(",").Select(s => s.Trim()).ToArray(); // Split every element by the commas, and remove any whitespaces 

But if you need to parse more complicated jsons, then you need something like this answer: https://stackoverflow.com/a/24891561/2420998

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

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.