0

I'm a novice developer for WP. I have some problems with parsing json data. I take it from the server and parse with JSON.Net.

Example of JSON data

{"response":
     {"ad6a95dd8f90fad7e281994cb5a8cacd":
           {"status":"offline", "name": "Test Name",
            "id":"ad6a95dd8f90fad7e281994cb5a8cacd"}
     }
 "success":true
}

The first chield of "response" varies with each request to the server. How I can extract value of "name" field ?! Thank you in advance for your reply.

I try that in Page.xaml.cs

var o = JObject.Parse(result);
        var id = o["response"].First;
        ServerList.ItemsSource = id;

and in Page.xaml

<ScrollViewer Foreground="White">
                        <ListBox Margin="0,0,-12,0" Name="ServerList" Height="508" Width="415">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Margin="0,0,0,20" Width="300">
                                        <TextBlock Text="{Binding Path=name}" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </ScrollViewer>

I did it by example.

3
  • Are you getting any error by using this code? this may help Commented Oct 19, 2012 at 15:32
  • I do not get anything in Listbox...and by using this code i'm getting var id ={"ad6a95dd8f90fad7e281994cb5a8cacd": {"status":"offline", "name": "Test Name", "id":"ad6a95dd8f90fad7e281994cb5a8cacd"} instead this {"status":"offline", "name": "Test Name", "id":"ad6a95dd8f90fad7e281994cb5a8cacd"} Commented Oct 19, 2012 at 15:54
  • What exactly do you want to do? SHow the name attribute? Using a ListBox is an overkill. Or do you want to show all the attribute values? If so, you need to to bind an IEnumerable. Commented Oct 19, 2012 at 17:15

1 Answer 1

1

To get the name value from that Json I did this:

var o = JObject.Parse(result);
var id = o["response"].First.First["name"];
string name = id.Value<string>();

If you are actually trying to do this operation over an array of responses such as this:

{"responses":
    [
        {"response":
            {"ad6a95dd8f90fad7e281994cb5a8cacd":
                {"status":"offline", "name": "Test Name","id":"ad6a95dd8f90fad7e281994cb5a8cacd"} },"success":true },
        {"response":
            {"ad6a95dd8f90fad7e281994cb5a8cacd":
                {"status":"offline", "name": "Test Name2","id":"ad6a95dd8f90fad7e281994cb5a8cacd"} }, "success":true }
    ]
}

Then your code would need to look something like:

var o = JObject.Parse(result);
var ids = from c in o["responses"].Children() select c["response"].First.First;
var names = ids.Select(t => t.SelectToken("name").Value<string>());
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.