0

I have a JSON and I need to get the values of 'pk' and 'cnt' from it:

[{
  "ip": "[{"pk": "173.194.44.23",
           "model": "link_finder.ip",
           "fields": {"cnt": 3}},
          {"pk": "173.194.44.24",
           "model": "link_finder.ip",
           "fields": {"cnt": 3}}]",
  "urls": "[{"pk": "http:\\google.com\advanced_search?hl=uk&authuser=0",
             "model": "link_finder.url",
             "fields": {"cnt": 2}},
            {"pk": "http:\\google.com\intl\uk\about.html",
             "model": "link_finder.url",
             "fields": {"cnt": 2}}]"
}]

I know how to put it in an array, but how do I get, for example, the pk and cnt walues from first and second paragraph ?

0

2 Answers 2

3

Updated because the code changed:

If the object is named ip, then:

data[0].ip[0].pk
data[0].ip[0].fields.cnt
data[1].ip[1].pk
data[1].ip[1].fields.cnt

Original answer:

You're missing the array brackets for this to be valid JSON.

Also assuming that you're getting this JSON from some sort of Ajax-y thing, and that it's sending back the JSON in an object called 'data'.

If you had the array brackets, the syntax would be:

data[0].pk //123.104.44.23
data[0].fields.cnt // 159


data[1].pk //171.154.44.24
data[1].fields.cnt // 159

This would be in Javascript, though looking at it that syntax would work for Python as well.

Sign up to request clarification or add additional context in comments.

3 Comments

@user3156971 I hope this aids in you understanding the basics of getting data from a JSON object. This is really basic stuff, if you've got more questions you should probably comment here.
Made it, heres the data
@user3156971 I updated my answer according to the JSON displayed.
1

Check the next code.

var json = {};
json =  [{"pk": "123.104.44.23",
  "model": "link_finder.ip",
  "fields": {"cnt": 159}},
 {"pk": "171.154.44.24",
  "model": "link_finder.ip",
  "fields": {"cnt": 159}},
 {"pk": "172.194.34.31",
  "model": "link_finder.ip",
  "fields": {"cnt": 159}}]; 
for (var key in json){ console.log(json[key].pk + ' ' + json[key].fields.cnt)}

Result:

123.104.44.23 159
171.154.44.24 159
172.194.34.31 159

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.