• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Problems with JSON Array can't become String

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm making this APP for my company, but I've never used JSON with Java before, so I'm kinda lost here. After watching some tutorials, I made this function


But the response from the URL that I get is a JSON with no name,

{
 "atividade_principal": [
   {
     "text": "Portais, provedores de conteúdo e outros serviços de informação na internet",
     "code": "63.19-4-00"
   }
 ],
 "data_situacao": "01/09/2004",
 "complemento": "ANDAR 17A20 TSUL 2 17A20",
 "tipo": "MATRIZ",
 "nome": "GOOGLE BRASIL INTERNET LTDA.", //I need this part

Can someone help me, please ? This array worked like a charm, how to get "nome" from this JSON ?



If anyone needs the whole JSON: https://www.receitaws.com.br/v1/cnpj/06990590000123

That's my Log:
org.json.JSONException: Value GOOGLE BRASIL INTERNET LTDA at nome of type java.lang.String cannot be converted to JSONArray

I thank you for the help, my native forums aren't helping me
 
Ranch Hand
Posts: 47
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marcus Vinícius wrote:Hello,

I'm making this APP for my company, but I've never used JSON with Java before, so I'm kinda lost here. After watching some tutorials, I made this function


That's my Log:
org.json.JSONException: Value GOOGLE BRASIL INTERNET LTDA at nome of type java.lang.String cannot be converted to JSONArray



The JSON string from the URL shows that you have an JSON object that is not an array. As immediate child there is the JSON value "nome" which has value "GOOGLE BRASIL INTERNET LTDA." and is definitely no array as the error message in the log says.

Probably, you should only retrieve the object with key "nome" and return it. following code is an untested guess:



 
Marcus Vinícius
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roland Mueller wrote:

Marcus Vinícius wrote:Hello,

I'm making this APP for my company, but I've never used JSON with Java before, so I'm kinda lost here. After watching some tutorials, I made this function


That's my Log:
org.json.JSONException: Value GOOGLE BRASIL INTERNET LTDA at nome of type java.lang.String cannot be converted to JSONArray



The JSON string from the URL shows that you have an JSON object that is not an array. As immediate child there is the JSON value "nome" which has value "GOOGLE BRASIL INTERNET LTDA." and is definitely no array as the error message in the log says.

Probably, you should only retrieve the object with key "nome" and return it. following code is an untested guess:





Thanks for the reply, but how would I use it in the function ? I did some search and Json is a library from Java and when I tried to use it in Android it didn't work  
 
Marcus Vinícius
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, I found the solution.

In this case, the JSON that I'm looking isn't an array, so I already started wrong.

After some search and help on the web, I finished it.



And this applies to any other String in JSON, I could make a .getString("stringname"); for each String in JSON with no worries !

Thank you again !
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic