Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I my php file, I need to make my own Json array.

        for($i=1;$i<$arraySize+1;$i++){
            $idJson[$i]=$i.":".$birdIDArray[$i-1];  
        }

        for($i=$arraySize+1 ;$i<$arraySize*2+1; $i++){
            $idJson[$i]=$i.":".$rankArray[$i-($arraySize+1)];
        }

When I use

print(json_encode($idJson));

the OUTPUT : ["0:3","1:15","2:3","3:14","4:1","5:2","6:2"]

But i need the output like this ["0":"3","1":"15","2":"3","3":"14","4":"1","5":2","6":"2"]

When I going to add " mark

        for($i=1;$i<$arraySize+1;$i++){
            $idJson[$i]=$i.'"'.":".'"'.$birdIDArray[$i-1];  
        }

        for($i=$arraySize+1 ;$i<$arraySize*2+1; $i++){
            $idJson[$i]=$i.'"'.":".'"'.$rankArray[$i-($arraySize+1)];
        }

It prints like this ["0:3","1\":\"15","2\":\"3","3\":\"14","4\":\"1","5\":\"2","6\":\"2"]

How can I avoid from printing this \ sign?

share|improve this question
1  
why not simply json_encode($your_array);..? why do you want to create custom...? –  DemoUser Oct 7 '13 at 10:09
    
Why don't you just make a normal php array and use json_encode? –  Console Oct 7 '13 at 10:09
1  
["0":"3", ... is not a valid JSON array! –  deceze Oct 7 '13 at 10:13
    
I cannot simply encode my array to json because I need ID and Value pair to get data to my application with Json Object. –  Shashika Oct 7 '13 at 10:19

1 Answer 1

up vote 1 down vote accepted

I'm assuming you want a JSON object like this:

{"0":"3", ... }

The problem here is that Javascript/JSON distinguishes between key-value pairs, which are objects, and numerically indexed lists, which are arrays, while PHP uses arrays for both these things. With json_encode it depends on whether the PHP array is a continuously numerically indexed array, in which case you get a JSON array, or anything else, in which case you get a JSON object.

What you want is to force a JSON object even for a continuously numerically indexed array. The first question here would be: why?! But if you're really sure you want that (again, why?!), there's the JSON_FORCE_OBJECT flag in PHP 5.3+:

echo json_encode(array("3", "15", "3"), JSON_FORCE_OBJECT);
// {"0":"3","1":"15","2":"3"}

But I'll say again that that's pretty pointless. If you use a regular array like ["3","15","3"], the keys to those elements are already implicitly 0, 1 and 2. There's no need to enforce them as object keys.

share|improve this answer
    
JSONArray jarray=new JSONArray(result); for(int i=0;i<jarray.length();i++){ JSONObject json=jarray.getJSONObject(i); len=json.getString("0"); } previously I used json.getString(), but what can I use in here to get data from key0. –  Shashika Oct 7 '13 at 10:46
    
I have no idea what you're trying to tell me here. –  deceze Oct 7 '13 at 10:47
    
How can I get values in json array by calling the key(position of the array)? –  Shashika Oct 7 '13 at 10:50
    
In Android's Java? No idea. I can tell you that a JSON array ([3, 15, 3, ...]) seems to make the most sense for your data structure. How you work with that in Java I don't know. –  deceze Oct 7 '13 at 11:12
    
It's worked. It's easy to get values through indexes of JSON array. –  Shashika Oct 7 '13 at 17:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.