0

I have a json.json file like this

{
"name1":"ts1=Hallo&ts2=Hillarry&ts3=Sting&ts4=Storm",
"name2":"st1=Hallo2&st2=Hillarry2&st3=Sting2&st4=Storm2",
"name3":"dr1=Hallo3&dr2=Hillarry3&dr3=Sting3&dr4=Storm3",
"name4":"ds1=Hallo4&ds2=Hillarry4&ds3=Sting4&ds4=Storm4"
}

And this script im using to read the file

<script type='text/javascript'>
$(window).load(function(){
$.getJSON("json.json", function(person){
    document.write(person.name3);
});
});
</script>

This script is made to point out all of the data from "name3" but i need only "dr3" value from "name3" to be stored to be written.

How to do that?

  • 1
    Related: [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Ram Sep 10 '14 at 12:57

6 Answers6

1

You can store it like this using combination of split() calls.

var dr3val = person.name3.split("&")[2].split("=")[1];
console.log(dr3val); // "Sting3"

The above will work if the order is same. Else you can use the below

var dr3val = person.name3.replace(/.*?dr3=(.+)?&.*/,"$1");
console.log(dr3val); // "Sting3"
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

You should change your json to this:

{
"name1":
     {
        "ts1" : "Hallo",
        "ts2" : "Hillarry", 
        "ts3" : "Sting",
        "ts4" : "Storm"
    }
}

this way it makes your jsonstring much easier to use.

Get the data from it like this:

person.name1.ts1
Stijn Bernards
  • 1,091
  • 11
  • 29
0
 var json = {
            "name1":"ts1=Hallo&ts2=Hillarry&ts3=Sting&ts4=Storm",
            "name2":"st1=Hallo2&st2=Hillarry2&st3=Sting2&st4=Storm2",
            "name3":"dr1=Hallo3&dr2=Hillarry3&dr3=Sting3&dr4=Storm3",
            "name4":"ds1=Hallo4&ds2=Hillarry4&ds3=Sting4&ds4=Storm4"
        };

        var name3 = json.name3.split('&');
        for (var i = 0; i < name3.length; i++) {
            if (name3[i].indexOf("dr3=") > -1) {
                var value = name3[i].replace("dr3=", "");
                alert(value);
            }
        }
marko
  • 10,684
  • 17
  • 71
  • 92
0

Implement this jQuery plugin i made for a similar case i had to solve some time ago. This plugin has the benefit that it handles multiple occurring variables and gathers them within an array, simulating a webserver behaviour.

<script type='text/javascript'>
(function($) {
    $.StringParams = function(string) {
        if (string == "") return {};
        var result = {},
            matches = string.split('&');
        for(var i = 0, pair, key, value; i < matches.length; i++) {
            pair = matches[i].split('=');
            key = pair[0];
            if(pair.length == 2) {
                value = decodeURIComponent(pair[1].replace(/\+/g, " "));
            } else {
                value = null;
            }
            switch($.type(result[key])) {
                case 'undefined':
                    result[key] = value;
                    break;
                case 'array':
                    result[key].push(value);
                    break;
                default:
                    result[key] = [result[key], value];
            }
        }
        return result;
    }
})(jQuery);
</script>

Then in your code do:

<script type='text/javascript'>
$(window).load(function(){
    var attributes3;
    $.getJSON("json.json", function(person){
        attributes3 = $.StringParams(person.name3)
        console.log(attributes3.dr3);
    });
});
</script>
wiesion
  • 2,349
  • 12
  • 21
0

Underscore solution:

_.map(json, function(query) {         //map the value of each property in json object
    return _.object(                  //to an object created from array
        query.split('&')              //resulting from splitting the query at & 
        .map(function(keyval) {       //and turning each of the key=value parts
            return keyval.split('='); //into a 2-element array split at the equals.
    );
})

The result of the query.split... part is

[ [ 'ts1', 'Hallo'], ['ts2', 'Hillarry'], ... ]

Underscore's _.object function turns that into

{ ts1: 'Hallo', ts2: 'Hillarry', ... }

The end result is

{
    name1: { ts1: 'hHallo', ts2: 'Hillarry, ...},
    name2: { ...
}

Now result can be obtained with object.name3.dr3.

Avoiding Underscore

However, if you hate Underscore or don't want to use it, what it's doing with _.map and _.object is not hard to replicate, and could be a useful learning exercise. Both use the useful Array#reduce function.

function object_map(object, fn) {
    return Object.keys(object).reduce(function(result, key) {
        result[key] = fn(object[key]);
        return result;
    }, {});
}

function object_from_keyvals(keyvals) {
    return keyvals.reduce(function(result, v) {
        result[v[0]] = v[1];
        return result;
    }, {});
}
0

:

var person={
    "name1":"ts1=Hallo&ts2=Hillarry&ts3=Sting&ts4=Storm",
    "name2":"st1=Hallo2&st2=Hillarry2&st3=Sting2&st4=Storm2",
    "name3":"dr1=Hallo3&dr2=Hillarry3&dr3=Sting3&dr4=Storm3",
    "name4":"ds1=Hallo4&ds2=Hillarry4&ds3=Sting4&ds4=Storm4"
}     
var pN = person.name3;   
var toSearch = 'dr3';    
var ar = pN.split('&');    
var result = '';    
for(var i=0; i< ar.length; i++) 
  if(ar[i].indexOf(toSearch) >= 0 ) 
     result=ar[i].split('=')[1];
console.log('result=='+result);
Faizy
  • 21
  • 7