Can you read a json and get all the strings without knowing what’s inside?

Asked

Viewed 634 times

0

I’m making a dynamic form where I get one Json and create a dynamic form, everything is automated, except the part I have to extract each string of JSon and put inside a array.

  • 1

    What’s wrong with knowing what’s inside JSON? "string", for you, includes the keys of the objects inside JSON?

  • @daniel12345smith it’s okay not to know what the values are, as long as you know what the keys are. have how to post what you have so far to make it easier to help you?

  • "{"name":"name","last name":"last name","age":"age","address","address","parents":"parents"}" I’m using jo.getString("first name") for example, and putting inside a string vector, it’s like me putting in the vector without knowing if inside the json there would be a string "first name""?

1 answer

0

Think of it that way:

This is an object: { }, and that’s an array: []

Every "JSON" output is considered an object. Even the Javascript array is viewed with an object only of the Array type.

In this case, when you receive a collection in "JSON" with keys and assign a name to these keys: var obj = { nome: "x", id: "y" } the correct way to access its values "x" and "y" is to call the object this way: obj.name, obj.id. The same can be done differently, as is done in the array, however this is valid in cases where keys will be dynamic: obj["nome"]; obj["id"];

Now think that if you want to receive a JSON as a string, it is the same as ignoring all these rules. Then you just take the whole object, and convert it to a string, for that use the method JSON.stringify(obj);

  • Sorry, mate, but I disagree, {0: x, 1: y} is not interpreted with array, otherwise it would support .length unless you’re talking about technologies other than Javascript that try to interpret Json. Another thing, what the author asked was how to extract json with android so probably his doubt is about Java and not about the difference of Object and Array, I suggest you edit the reply friend.

  • I agree, I’ve edited the text, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.