1

I am having trouble with json formation for an api call. I need something like this

{
    "token": "87dd8f93-27ad-493c-8ab1-e75c50b8fb71",
    "answers": [
        {
            "question": "Where are you from",
            "ans": "t"
        },
        {
            "question": "I am from tts",
            "ans": "f"
        }
    ]
}

i have array of hashes named answers which i made separately with something like

string json = JsonConvert.SerializeObject(account, Formatting.Indented);

after that i have to make with the token but then using the same process i got

{
    "token": "87dd8f93-27ad-493c-8ab1-e75c50b8fb71",
    "answers": [
        "{\r\n
            \"question\": \"Where are you from\",
            \"ans\": \"t\"
        }",
        "{\r\n
            \"question\": \"I am from tts\",
            \"ans\": \"f\"
        }"
    ]
}

 public class Account
    {
       public string question { get; set; }
       public string ans { get; set; }           
    }

and after that

if (ansNo.IsChecked == true)
                    {
                        Account account = new Account
                        {
                            question = quizText.Text,
                            ans = "f"
                        };
                        string json = JsonConvert.SerializeObject(account, Formatting.Indented);
                        Globals.answers[counter] = json;   
                    }
                    else
                    {
                        Account account = new Account
                        {
                            question = quizText.Text,
                            ans = "t"
                        };
                        string json = JsonConvert.SerializeObject(account, Formatting.Indented);
                        Globals.answers[counter] = json;
                    }

need help please Thanks

12
  • 1
    If you're just passing it into an api call, why do you care if it's indented? The function that you're calling certainly doesn't :) Commented Feb 28, 2014 at 14:52
  • @LynnCrumbling because due to '"' on curly braces in answer it is not proper json, and not getting any result back Commented Feb 28, 2014 at 14:54
  • @L.B it is adding quotes on curly braces in answers. Commented Feb 28, 2014 at 14:55
  • How do you fill account? Commented Feb 28, 2014 at 14:56
  • @user2752370 What does that have to do with the indented format? Commented Feb 28, 2014 at 15:02

1 Answer 1

1

I think what you are after can be achieved by a rejig of your data. Create a new object to serialise to return

public class MyJson{
    public string token {get;set;}
    public List<Account> answers {get;set;}

    public MyJson(){
        answers = new List<Account>();
    }
}

Create a new MyJson object and add the token

MyJson o = new MyJson { token = "87dd8f93-27ad-493c-8ab1-e75c50b8fb71" }

Then like you already have add to the answers to the list

Account account = new Account{
     question = quizText.Text,
     ans = "t"
};
o.answers.Add(account);

Then serialise the whole thing and return it

return JsonConvert.SerializeObject(o, Formatting.Indented);
Sign up to request clarification or add additional context in comments.

9 Comments

Adwin thanks for the example. I have tried it. but having an issue. The answers list doesn't getting any thing. its empty ....
account object as hashes? not sure what you mean by that... The account list object should populate in the if (ansNo.IsChecked == true) if statement, there should be an item added if that if statement is reached.
Adween yap i tried that in conditional statements and by hashes i mean i have a question text like ("where are you from") and an answer like ("t" or "f"). i want them as hashes like {"question":"where are you fro","ans":"t"}, and all hashes in one array and then that array and token is one parent hash.
Ahh ok, To make them hashes i would use a Dictionary<string, string> answers {get;set;} instead of a of list of account. then yes they would be hashed, however why do you need them to be hashed? it doesn't really matter when they are sent as json...
ok got your point about hashes but it is not adding account in answers list .... got any idea ?
|

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.