0

I am after something like this:

I have multiple lists in C# like so:

List<string> Names = new List<string>()
{
    "Name1",
    "Name2",
    "Name3"
};

List<double> Values = new List<double>()
{
    1,
    2,
    3
};

I want to serialize them into an array in javascript (I am actually looking to return a string) to get something like this:

[
{ name: "Name1", value: 1 },
{ name: "Name2", value: 2 },
{ name: "Name3", value: 3 }
]

So far I have tried this:

public class bmData
{
    string _name;
    public string name
    { 
        get { return this._name; } 
        set { this._name = value; }
    }
    double _value;
    public double value
    {
        get { return this._value; }
        set { this._value = value; }
    }
}

zip them together and wrap into a "data item" class. I want to keep this functionality for other reason. Then I zipped the key value pairs into this new class:

List<bmData> namesValues = Names.Zip(Values, (x, y) => new bmData{name = x, value = y}).ToList();

Finally I tried to serialize it like so:

var serializer = new JavaScriptSerializer();
string jsData = serializer.Serialize(namesValues);

But this return something like this:

[
{"name":"Name1","value":1},
{"name":"Name2","value":2},
{"name":"Name3","value":3}
]

It's those extra "" around name and value that are killing the key:value association. Any ideas?

Thank you!

5
  • In JSON, the key names are string types and as such are supposed to have double-quote characters around them. I.e. the JavaScriptSerializer is doing exactly what it's supposed to. Why do you think that the key names should not have double-quotes? Commented Oct 2, 2015 at 22:46
  • because i am not looking to get a JSON string. I am building an HTML string from this and this part represents a piece of javascript array. Commented Oct 2, 2015 at 22:50
  • If you don't want JSON, then why do you think you can get the string you want by using a JSON serializer? If you want to use a JSON serializer but don't like its output, why don't you just post-process its output to remove the double-quote characters you don't want? Commented Oct 2, 2015 at 22:55
  • I am not exactly sure how to achieve what I need. These are just things that I have tried and results that i was able to get. I am also listing a result that I would like to get in hope of some constructive feedback. Thanks! Commented Oct 2, 2015 at 22:59
  • 1
    If you would like an answer, please provide a good, minimal, complete code example that shows clearly what you've tried, along with a detailed description of what that code does and how that's different from what you want it to do. All you've posted so far is code that seems to be behaving exactly as it should. Please show what effort you've made to post-process the correct output from the JSON serializer to suit your needs, and explain specifically what problem you've had in getting that effort to work correctly. Commented Oct 2, 2015 at 23:03

1 Answer 1

1

You can achieve what you need using Json.NET serializer. Check out the next code and change it according to your needs:

List<bmData> namesValues = new List<bmData>
{
    new bmData { name = "Name1", value= 1 },
    new bmData { name = "Name2", value= 2 },
    new bmData { name = "Name3", value= 3 }
};

string jsData = "[]";
var serializer = new JsonSerializer();
using (var stringWriter = new StringWriter())
{
    using (var writer = new JsonTextWriter(stringWriter))
    {
        writer.QuoteName = false;
        serializer.Serialize(writer, namesValues);
    }

    jsData = stringWriter.ToString();
}

However, I have to remark that this will produce an invalid JSON, but it's what you need.

Result:

[
{name:"Name1",value:1.0},
{name:"Name2",value:2.0},
{name:"Name3",value:3.0}
]
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.