0

Im trying to find an answer for this but i must be searching for the wrong terms.

Im working on a Windows phone app and am getting data from an API with a nested array value "user.username"

void data_arrived(object sender, DownloadCompleteData e)
    {
        String data = e.data;
        JArray obj = JArray.Parse(data);
        for (int i = 0; i < obj.Count; i++)
        {

            JObject row = JObject.Parse(obj[i].ToString());
            var item = new DataList();
            item.country = row["title"].ToString() + " (€" + row["price"].ToString() + ") ";
            item.code = row["price"].ToString();
            item.imageURL = row["urlimage"].ToString();
            item.votes = row["votes"].ToString();
            item.category = row["category"].ToString();
            item.username = row["user.username"].ToString();
            list.Items.Add(item);
        }
    }

Everything else works fine except user.username

How do i use this properly?

Thanks

4
  • Can you post your actual json? Commented Mar 24, 2016 at 17:50
  • yeah no problem saveme.ie/api/savings Commented Mar 24, 2016 at 17:52
  • Are you using google apis? This looks a lot like them. Commented Mar 24, 2016 at 18:13
  • No just my own through Node.js Commented Mar 24, 2016 at 18:23

2 Answers 2

1

You can deserialize a valid JSON string to a dynamic object. This will allow you access to underlying object using dot notation. e.g.

dynamic row = JsonConvert.DeserializeObject (obj[i].ToString());

Your final code block inside loop will look like

dynamic row = JsonConvert.DeserializeObject(obj[i].ToString());

Console.WriteLine(row.title.ToString() + " (€" + row.price.ToString() + ") ");
Console.WriteLine(row.price.ToString());
Console.WriteLine(row.urlimage.ToString());
Console.WriteLine(row.votes.ToString());
Console.WriteLine(row.category.ToString());
Console.WriteLine(row.user.username.ToString());

Console.WriteLine();
Console.WriteLine("-----------------------------\n");
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. So do i replace "JObject row = JObject.Parse(obj[i].ToString()); " with "dynamic row = JsonConvert.DeserializeObject (obj[i].ToString()); " or do i add the "dynamic row = JsonConvert.DeserializeObject (obj[i].ToString());" at the end of whats there already?
nop, replace. and read and populate your item object the way I suggested in last line of code block.
Throwing and error "Additional information: 'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'title'"
Sorry. Its still throwing the error. 'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'title'"
0

There is no "easy" way to achieve this because the . in C# is reserved.

However, you could achieve something pretty close by using a dictionary and collection initializer. It's still somewhat isolated, and doesn't require you to create a custom class.

var obj = new Dictionary<string, object>
{
    { "user.username", "myvalue" }
};

var json = JsonConvert.SerializeObject(obj);
//{"user.username":"myvalue"}

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.