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

6 Answers 6

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"
Sign up to request clarification or add additional context in comments.

2 Comments

Actually this will fail if they come in different order. Better to check for the querystring parameters.
what if the dr3 is like this dr3=Dallas|spoiler=hls how to take only Dallas ??
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

Comments

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);
            }
        }

Comments

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>

Comments

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;
    }, {});
}

Comments

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);

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.