简体   繁体   中英

JSON Parsing in Android for Strings Without Label

I have problem when trying to parse this JSON.

{
    "bakso-roso-n'deso__-6.19_106.77":
    {"Latitude":"-6.185488","Longitude":"106.77366","Distance":"90.89210930799594"},
    "print-point-duri-kepa__-6.19_106.77":
    {"Latitude":"-6.18599544813468","Longitude":"106.772603988647","Distance":"118.9849339548274"},
    "apartment-menara-kebun-jeruk__-6.19_106.78":
    {"Latitude":"-6.18530376709007","Longitude":"106.775222279179","Distance":"247.8816947606767"},
    "ranch-market---pesanggrahan__-6.19_106.77":
    {"Latitude":"-6.18761306472002","Longitude":"106.77343661177","Distance":"294.4255786871916"}
}

The problem is how I get "bakso-roso-n'deso__-6.19_106.77" as string. It doesn't have any tag.

Thank you.

Now regular way to handle Json is like this:

String jstring = "{\"menu\": {\"id\": \"file\", \"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";
try
{
    jObject = new JSONObject(jstring);

How would I do that for this type of json? What should jstring be?

Basically I want to turn these json string into some dictionary.

plz check this string this is not a valid json String..

 String jstring = "{\"menu\": {\"id\": \"file\", \"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";

check the json is valid or not http://jsonlint.com/

Actually your "bakso-roso-n'deso__-6.19_106.77" is the tag (key ?) itself..

JSONObject jsonObject = new JSONObject(YOUR_RESPONSE_STRING);
Iterator<String> myIter = jsonObject.keys();
while(myIter.hasNext()){
  //here you can get all keys
}

I think you can get the iterator over keys using jObject.keys();

as per your code ....

String jstring = "{\"menu\": {\"id\": \"file\", \"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";


  try
      {
         jObject = new JSONObject(jstring);
         Map<String,String> map = new HashMap<String,String>();
         Iterator iter = jObject.keys();

         while(iter.hasNext()){
            String key = (String)iter.next();
            String value = menu.getString(key);
            map.put(key,value);
        }
    } catch(exception e){}

Parse JSON object with string and value only

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM