2

I have a simple php script that outputs an array of strings using the json_encode method at the backend. it outputs it like so:

["8090123","8090456","8090789","8090321","8090654"]

Now, I want to parse it in my C# code but I can't seem to parse it properly.

Below is my C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WebClient wc = new WebClient())
            {
                string myString = wc.DownloadString("https://magforex.biz/list.php");
                string[] words = myString.Split(',');
                for (var i = 0; i < words.Length; i++)
                {
                    // i just hardcoded the index for now
                    Console.WriteLine(words[0]); 
                    break;
                }
                Console.ReadLine();
            }  
        }
    }
}

The code runs just fine but it outputs the following result ["8090123" which is not what I wanted.

Sorry as I am just starting out with C# and has limited knowledge about it.

All I want is just the string 8090123 so that I can do a comparison later.

Thanks for helping out in advance.

2
  • 1
    Use a JSON library like JSON.NET Commented Feb 28, 2016 at 13:47
  • 1
    Use a JSON library, you don't want to parse it manually. Commented Feb 28, 2016 at 13:54

5 Answers 5

5

Use a proper JSON-parsing library like Newtonsoft.Json to parse the JSON string into a statically typed class model:

string[] words = JsonConvert.DeserializeObject<string[]>(myString);
Sign up to request clarification or add additional context in comments.

Comments

2

You're just missing one step:

string[] words = myString.Trim(new []{'[',']'}).Split(',');

Honestly, unless you're going to use JsonConvert for more complex object graphs, using a json deserializer is a bit of an overkill in this situation, IMHO.

EDIT: if you don't want the quotation marks, you could use a LINQ projection...

string[] words = myString.Trim(new []{'[',']'})
                     .Split(',')
                     .Select(s => s.Trim(new[]{'"'}).ToArray();

BTW, I'm highly familiar with Json.NET and have been a ServiceStack guy for ages. But I'm biasing towards the simpler solution because, well, Occam's Razor, right?

5 Comments

you might be right. its just a simple array that i want to parse so I thought by just using the split method will do the trick. but the json library as suggested worked for my immediate need. I 'm not sure if its the right one for my simple task.
You were just missing the Trim step to cleanse the data prior to splitting... ;-)
This method would still leave the quotes around the individual strings, so you would need a loop to strip those off too. Using a JSON parser is a better way to go.
Just trim out what you don't want.. I thought they wanted the quote marks, TBH...
You'd have to trim the quotes after splitting values, otherwise only the first and last quote in the full string would be removed. Something like string[] words = myString.Trim(new char[]{'[',']'}).Split(',').Select(s => s.Trim(new char[]{'"'})).ToArray();
1

Why not do the obvious thing and JSON-decode your JSON input data? Using Newtonsoft.Json:

JsonConvert.DeserializeObject<string[]>(myString)

Comments

1

If you don't want to use the Newtonsoft library you can use the JavaScriptSerializer to convert your string to array. Add using System.Web.Script.Serialization; and then:

string myString = wc.DownloadString("https://magforex.biz/list.php");
JavaScriptSerializer js = new JavaScriptSerializer();
string[] myArray = js.Deserialize<string[]>(myString);

Comments

0

you should change it to word[i] to start, then I'm not sure exactly what you are trying to do.

3 Comments

yes I know. I just hardcode the index for now so I can prove if I am parsing the correct index or not before I run the loop. thanks ;)
then you probably want words.Join()
its ok. the json library as suggested by others worked! thanks anyway ;)

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.